• 0

C++ End of Line problem


Question

Hi everyone,

I am getting stuck while trying to get something to work in

my program.

I have tab-delimited text file with its first row containing

variable names (columns). The rest of the rows contain

data (integers).

At first, I just wanted to skip the first line to determine the number of rows and columns. I skipped it using:

  // Skip variable names   
		   while(j!=2){			   
		   temp = inStream.get();   
		   if(temp=='\n') j=2;		  
		   }

and my instream and outstream are defined as:

	 ifstream inStream; ofstream outFile;

Later in the program, I close and open the data file to

read in the first line, because I want to get the variable names

to print them in the new data file with adjusted data.

I tried this:

  // Open output file in advance
ofstream output("c:/adjusted.txt", ios::app);

 // Check whether input file was opened succesfully
 	if (output.fail()) {
	cout << "Failed to open/create output file";
	exit(1);
  }

 j=0;

  while(j!=2){
			  temp2 = inStream.get();
			  if(temp2=='\n') j=2;
			  output << temp2;
			  }

It compiles just fine, but when I run it, it keeps running as

if the loop doesn't break, but I don't know why :eek:

It fills my new text file with looooads of "??????????????????"

Does anyone have a clue why this is happening?

Thank you very much in advance,

Dani?l

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

To skip the first line, you can just use a temporary string to hold the entire line:

string tmp;
tmp = inStream.getline();

I think it's best to grab the entire textfile, line by line, into a string (using the technique above). That way, you don't need to seek the entire line character by character until you find a '\n'. Then you can search each line as a string for tabs, if you need.

And a last thing, why are you using exit(1); ? Wouldn't using a return value make more sense?

(e.g. change exit(1); to return 1;)

Link to comment
Share on other sites

  • 0
To skip the first line, you can just use a temporary string to hold the entire line:

string tmp;
tmp = inStream.getline();

I think it's best to grab the entire textfile, line by line, into a string (using the technique above). That way, you don't need to seek the entire line character by character until you find a '\n'. Then you can search each line as a string for tabs, if you need.

And a last thing, why are you using exit(1); ? Wouldn't using a return value make more sense?

(e.g. change exit(1); to return 1;)

Thank you for your reply. But if I try to store the whole line in a string variable, I get the following error:

no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&)'

Link to comment
Share on other sites

  • 0
Thank you for your reply. But if I try to store the whole line in a string variable, I get the following error:

no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&)'

Ah, alternatively, you can try to use the getline function:

string currentLine;
getline(inStream, currentLine);

You can run that code through a loop while the inStream.eof() flag is false.

Link to comment
Share on other sites

  • 0
Ah, alternatively, you can try to use the getline function:

string currentLine;
 getline(inStream, currentLine);

You can run that code through a loop while the inStream.eof() flag is false.

That would be the way I would recommend doing it and just look for a '\n' character and then skip the line if its at the start.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.