Reading in new line character from a file.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 3
Reputation: sarah_ella is an unknown quantity at this point 
Solved Threads: 0
sarah_ella sarah_ella is offline Offline
Newbie Poster

Reading in new line character from a file.

 
0
  #1
Oct 22nd, 2008
Hi, I'm trying to read a series of ascii characters from a text file as their corresponding decimal integers. However when I try to read in the # 26 it is read in as a # 10 and no further characters after this are read. I've found out that # 26 represents a substitution character and # 10 is a new line (http://www.asciitable.com/)

This is the code I'm using to illustrate my problem;

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. int main()
  8. {
  9. // fill up the vector with 4 integers
  10. vector <int> vec;
  11. vec.push_back(21);
  12. vec.push_back(23);
  13. vec.push_back(26);
  14. vec.push_back(29);
  15. cout << "Numbers in vector are:" << endl;
  16. for (int i = 0; i < vec.size(); i++) {
  17. cout << vec[i] << " ";
  18. }
  19. //write out the vector contents as chars to a text file
  20. ofstream outfile("test.txt");
  21. if (outfile.fail()) {
  22. cout << "test.txt"
  23. << endl;
  24. return 1;
  25. }
  26. for (int g = 0; g < vec.size(); g++) {
  27. outfile << char(vec[g]);
  28. }
  29. outfile.close();
  30. //now read the chars back in as the ints [21, 23, 26, 29]
  31. string str, tmp;
  32. ifstream infile("test.txt");
  33. if (infile.fail()) {
  34. cout << "Error opening test.txt"
  35. << endl;
  36. return 1;
  37. }
  38. while (!infile.eof()) {
  39. getline(infile, tmp);
  40. str += tmp;
  41. str += "\n";
  42. }
  43. infile.close();
  44. cout << endl << "String read in is:" <<endl;
  45. for (int g=0; g<str.size(); g++) {
  46. cout << int(str[g]) << " ";
  47. }
  48. cout << endl;
  49. }

Any ideas what I can do?

Thankyou for any reply.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading in new line character from a file.

 
0
  #2
Oct 22nd, 2008
The input file is opened in text mode, which means your program will never see the '\n' character(s) because getline() will filter it out, leaving only the characters up to but not including '\n'.

That read loop is wrong anyway because eof() doesn't work that way.
  1. while (getline(infile, tmp)) {
  2. str += tmp;
  3. str += "\n";
  4. }

I don't know what you mean by reading # 26. Does that file contain binary information? If yes, then it isn't a text file.
Last edited by Ancient Dragon; Oct 22nd, 2008 at 7:05 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: sarah_ella is an unknown quantity at this point 
Solved Threads: 0
sarah_ella sarah_ella is offline Offline
Newbie Poster

Re: Reading in new line character from a file.

 
0
  #3
Oct 22nd, 2008
It isn't the number 26, its the ascii character that has a decimal value 26. This is written to a text file but is read back as the decimal value 10.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading in new line character from a file.

 
0
  #4
Oct 22nd, 2008
test.txt is a binary file, not a text file so getline() doesn't know how to read it. Then you are putting all that binary stuff into a std::string and ucing cout to display it -- cout can't because it doesn't know how to display binary data when converted to char.

Use a hex editor on that test.txt file and it contains the 4 values that were written.

Maybe this is what you want?
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. using namespace std;
  7. int main()
  8. {
  9. // fill up the vector with 4 integers
  10. vector <int> vec;
  11. vec.push_back(21);
  12. vec.push_back(23);
  13. vec.push_back(26);
  14. vec.push_back(29);
  15. cout << "Numbers in vector are:" << endl;
  16. for (size_t i = 0; i < vec.size(); i++) {
  17. cout << vec[i] << " ";
  18. }
  19. //write out the vector contents as chars to a text file
  20. ofstream outfile("test.txt");
  21. if (outfile.fail()) {
  22. cout << "test.txt"
  23. << endl;
  24. return 1;
  25. }
  26. for (size_t g = 0; g < vec.size(); g++) {
  27. outfile << char(vec[g]);
  28. }
  29. outfile.close();
  30. vec.erase(vec.begin(),vec.end());
  31. //now read the chars back in as the ints [21, 23, 26, 29]
  32. ifstream infile("test.txt", ios::binary);
  33. if (infile.fail()) {
  34. cout << "Error opening test.txt"
  35. << endl;
  36. return 1;
  37. }
  38. char c;
  39. std::string str;
  40. while (infile.good()) {
  41. c = infile.get();
  42. std::stringstream sstm;
  43. sstm << c;
  44. str += sstm.str();
  45. }
  46. infile.close();
  47. cout << endl << "String read in is:" <<endl;
  48. for (size_t g=0; g<str.size(); g++) {
  49. cout << int(str[g]) << " ";
  50. }
  51. cout << endl;
  52. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: sarah_ella is an unknown quantity at this point 
Solved Threads: 0
sarah_ella sarah_ella is offline Offline
Newbie Poster

Re: Reading in new line character from a file.

 
0
  #5
Oct 22nd, 2008
Ah, thanks, I understand now
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC