943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4322
  • C++ RSS
Oct 22nd, 2008
0

Reading in new line character from a file.

Expand Post »
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;

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarah_ella is offline Offline
3 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Reading in new line character from a file.

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 22nd, 2008
0

Re: Reading in new line character from a file.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarah_ella is offline Offline
3 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Reading in new line character from a file.

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?
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 22nd, 2008
0

Re: Reading in new line character from a file.

Ah, thanks, I understand now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarah_ella is offline Offline
3 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Write from CPP program to serial port
Next Thread in C++ Forum Timeline: factorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC