C++ using strcpy() and strtok() issues

I don't think I'm using strcpy() correctly...

Page 1 of 1

2 Replies - 8597 Views - Last Post: 12 October 2006 - 11:23 AM Rate Topic: -----

#1 trista  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 06

C++ using strcpy() and strtok() issues

Posted 12 October 2006 - 09:10 AM

I'm trying to copy the string, tokanize it and check the first token to see if it is a command or not. since the last part isn't working, I figured I would look to see if newString is copying to tempString. I don't think it is. When I try to printf() only the % sign shows. yAny ideas?

Here is the function, the entire code follows:

void lineEditor::processLine(char *newString)
{
  char *temp = NULL;

  strcpy(newString, tempString);

  temp = strtok(tempString, " ");
  while(temp != NULL) {
	printf("temp is \"%s\"\n", temp);
	temp = strtok(NULL, " ");
  }
  
  if(tempString[0] == isCommand())
	{
	  com = tempString[0];
	  cout << "com in lineEditor: " << com << endl;
	}
  else
	{
	  myList.insert(cIter,newString);
	}
 

 
}  

 




Here is the complete program (NOTE: ITS NOT COMPLETE, while loop won't end yet because the command isn't working):


#ifndef lineEditor_h
#define lineEditor_h
#include <list>
#include <iterator>
#include <iostream>
#include <string>

using namespace std;

class lineEditor
{
 public:
  //  lineEditor();
  // lineEditor(char nextString);
  void writeToFile(fstream&) const;
  void readFromFile();
  void runEditor();
  int isNumber(char *tok);
  bool isCommand();
  int insertLine(char nextString);
  int deleteLine(char nextString);
  void append(char nextString);
  void listLines();
  void processLine(char *newString);

 private:
 
  list<string>::iterator cIter;//traverse and access list members
  list<string>  myList;  //list container to hold strings
  int curLine;	  //keep track of the current line
  int maxLine;	  //keep track of total lines
  char com;		   //to return command
  char newString[255]; //bring in new string
  char tempString[255]; //work on newString

};

#endif

  
#include "lineEditor.h"
#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <fstream>

using namespace std;


void lineEditor::readFromFile() 
{
  ifstream infile;
  infile.open("myList.txt");
  if(infile.is_open())
	{
	  while(!infile.eof())
	{
	 infile.getline(newString, 255, '\n');
	 myList.push_back(newString);
	}
	  cout << "success!" << endl;
	  infile.close();
	}
  else
	{ 
	  cout << "No file available." << endl;
	}
  curLine = myList.size();
}
void lineEditor::writeToFile(fstream&) const
{}
void lineEditor::runEditor()
{
  cout << "Edit testfile" << endl; 
  cout << curLine << "> ";

  cin.getline(newString,255, '\n');
  cin.clear();
  processLine(newString);
   while(com != 'E')
	{
	  switch (com)
	{
	case 'I':
	  {
		break;
	  }//end 'I'
	case 'D':
	  {
		break;
	  }//end 'D;'
	case 'A':
	  {
		break;
	  } //end 'A'
	case 'L':
	  {
		listLines();
	  } //end 'L'
		break;
	  
	default: myList.insert(cIter, newString);
	} //end of switch
	  cin.getline(newString,255, '\n');
	  cin.clear();
	  processLine(newString);
	  cout << curLine << "> ";
	} //end of while
} //end runEditor
int lineEditor::isNumber(char *tok)
{} //stubbed off for now
bool lineEditor::isCommand()
{
  bool is = false;
  char temp;
  if(temp == 'I' || temp == 'i' || temp == 'D' || temp == 'd' || temp == 'A' || temp == 'a' || temp == 'L' || temp == 'l' || temp == 'E' || temp == 'e')
   {
	 is = true;
   }
  return is;
} //end bool
int lineEditor::insertLine(char nextString)
{} //stubbed off for now
int lineEditor::deleteLine(char nextString)
{} //stubbed off for now
void lineEditor::append(char nextString)
{}  //stubbed off for now
void lineEditor::listLines()
{
  for(cIter=myList.begin(); cIter != myList.end(); cIter++)
	{
	  cout << *cIter<<endl;
	}
} //end listLines
void lineEditor::processLine(char *newString)
{
  char *temp = NULL;

  strcpy(newString, tempString);

  temp = strtok(tempString, " ");
  while(temp != NULL) {
	printf("temp is \"%s\"\n", temp);
	temp = strtok(NULL, " ");
  } //end while
  
  if(tempString[0] == isCommand())
	{
	  com = tempString[0];
	  cout << "com in lineEditor: " << com << endl;
	} //end if
  else
	{
	  myList.insert(cIter,newString);
	} //end else
  
}  //end process lines

#include "lineEditor.h"
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <iterator>

using namespace std;

int main()
{ 
  lineEditor editor;   //object to access functions
  editor.readFromFile();  //begin the process
  editor.listLines();  //test to see if the lines came in
  editor.runEditor(); //now begin running the editor

  return 0;
}



Thank you for any help you can give!

Trista

Is This A Good Question/Topic? 0
  • +

Replies To: C++ using strcpy() and strtok() issues

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: C++ using strcpy() and strtok() issues

Posted 12 October 2006 - 09:28 AM

I think the problem is that you have your variables reversed in the strcpy function. Since the values you want to copy are stored in the newString variable based on your function declaration. void lineEditor::processLine(char *newString)

The string you are copying needs be the second variable not the first. So the value in newString will be copied to tempString in the following example.

strcpy(tempString, newString);


Was This Post Helpful? 0
  • +
  • -

#3 trista  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 06

Re: C++ using strcpy() and strtok() issues

Posted 12 October 2006 - 11:23 AM

Exactly right. Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1