By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

read last character from a file

P: 33
Hello,

The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me know for next time.

I am reading strings from a file to a vector, doing some "operations" on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last string is read twice (because I read using while(inputFile.good()){...} ).

So when I am rewriting my new file, I read from the vector and write to a txt file, and since the last string is recorded twice, it is then written twice. I would like to avoid that by checking if the last character of the file is not "\n". Also, since I'm already making a pass reading with strings, I would like to avoid making a second using getc() and check its final value. Avoiding the first pass with strings is not an option.

A temporary solution could be to compare the last 2 strings if they are equal, but this is risky.

I have thought about trying to point to the last character, but I'm lacking knowledge. I saw in the help methods like tell()s and seek()s, but it's not quite clear to me how they can be useful (they return "strange" datatypes).

As always, any thoughts/advices/help would be appreciated,

Ras.
Jun 15 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You should be able to use getline():
Expand|Select|Wrap|Line Numbers
  1. ifstream input("data.txt");
  2. char buffer[256];
  3. input.getline(buffer, 256);
  4.  
getline() converts the \n to a null terminator in buffer so you don't have to worry about it.
Jun 15 '07 #2

P: 33
You should be able to use getline():
Expand|Select|Wrap|Line Numbers
  1. ifstream input("data.txt");
  2. char buffer[256];
  3. input.getline(buffer, 256);
  4.  
getline() converts the \n to a null terminator in buffer so you don't have to worry about it.

Let me put it like this:
I have a multiple lines txt file, with different strings separated with spaces (it could be assimilated to a normal text).

I want to put all these strings in a vector, so I use
Expand|Select|Wrap|Line Numbers
  1. fileInput >> temp;
  2. vecFileContent.push_back(temp); 
  3.  
I perform some operations on those strings using my vector.
Then, I want to rewrite a new file, so I copy the content of my vector to that file.

Maybe I didn't get you right, but getline() is not helping me out because I still need to use .operator>>() method to fill my vector. The problem occurs because of the \n at the end of the file.

Your time is always appreciated,

Ras.
Jun 18 '07 #3

Savage
Expert 100+
P: 1,764
Let me put it like this:
I have a multiple lines txt file, with different strings separated with spaces (it could be assimilated to a normal text).

I want to put all these strings in a vector, so I use
Expand|Select|Wrap|Line Numbers
  1. fileInput >> temp;
  2. vecFileContent.push_back(temp); 
  3.  
I perform some operations on those strings using my vector.
Then, I want to rewrite a new file, so I copy the content of my vector to that file.

Maybe I didn't get you right, but getline() is not helping me out because I still need to use .operator>>() method to fill my vector. The problem occurs because of the \n at the end of the file.

Your time is always appreciated,

Ras.
You could read a whole line by using getline and then tokenize it and push it into vector.

Savage
Jun 18 '07 #4

P: 33
You could read a whole line by using getline and then tokenize it and push it into vector.

Savage

got it, thanks.

Ras.
Jun 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.