943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2127
  • C++ RSS
Aug 13th, 2007
0

problems with reading in file

Expand Post »
Hello there, I have here a program that reads a txt file taking each word in three columns separated by a comma and storing it to a vector. It has a filter that whenever it sees a comma, it seperates the word from each other. My problem is, whenever I type a phrase or a sentence in a line, the space is considered to a be a beginning of a new line in which ruining the output and producing a run time error. Here's my code and my input txt file.

CODE:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <cctype>
  4. #include <string>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <stdlib.h>
  8. #include <windows.h>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. vector<string> split_String(const string& str) //reading csv file
  15. {
  16. vector<string> vStr;
  17. int i = 0;
  18. while (i != str.size())
  19. {
  20. while (i != str.size() && str[i]==',' ){
  21. i++;
  22. }
  23.  
  24. int j = i;
  25. while (j != str.size() && str[j]!=',' ){
  26.  
  27. j++;
  28. }
  29.  
  30. if (i != j)
  31. {
  32. vStr.push_back(str.substr(i, j - i));
  33. i = j;
  34. }
  35.  
  36. }
  37.  
  38. return vStr;
  39.  
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. vector<string> vCode;
  46. vector<string> vWord;
  47. vector<string> vFile;
  48. std::string my_File("sample.txt");
  49. ifstream inFile(my_File.c_str());
  50. std::string str1;
  51. const char *pStorage = 0;
  52. int arr = 0;
  53.  
  54. if (!inFile) //if file cannot be read
  55. {
  56. cout << "Unable to open file";
  57. exit(1);
  58. }
  59. else
  60. {
  61. while(!inFile.eof()) // reading the file line by line
  62. {
  63.  
  64. inFile >> str1;
  65. pStorage = str1.data();
  66. cout << "BEFORE = " << pStorage << endl;
  67. vector<string> vOutput = split_String(pStorage);
  68.  
  69.  
  70. for (int i = 0; i < vOutput.size(); i++)
  71. {
  72. if(i == 0)
  73. {
  74. vCode.push_back(vOutput[i]);
  75. cout << "CODE = ";
  76.  
  77. }
  78. else if(i == 1)
  79. {
  80. vWord.push_back(vOutput[i]);
  81. cout << "WORD = ";
  82.  
  83. }
  84. else if(i == 2)
  85. {
  86. vFile.push_back(vOutput[i]);
  87. cout << "FILE = ";
  88.  
  89. }
  90.  
  91.  
  92. }
  93. cout << endl;
  94.  
  95. }
  96.  
  97.  
  98. }
  99. inFile.close();
  100.  
  101. ofstream outfile("output.bat");
  102.  
  103. for(int x =0; x < vCode.size(); x++)
  104. {
  105. outfile<<"sed -e 's/";
  106. outfile<<vCode[x];
  107. outfile<<"/";
  108. outfile<<vWord[x];
  109. outfile<<"/g' ";
  110. outfile<<vFile[x];
  111. outfile<<" > english/";
  112. outfile<<vFile[x];
  113. outfile<<"\n";
  114.  
  115.  
  116.  
  117. }
  118.  
  119.  
  120. outfile.close();
  121.  
  122.  
  123. system("output.bat");
  124.  
  125. return 0;
  126.  
  127.  
  128.  
  129. }

TEXT FILE INPUT:
C++ Syntax (Toggle Plain Text)
  1. STR-000,Login Name,login.html,
  2. STR-001,Password,login.html,
  3. STR-029,Login,login.html,
  4. STR-030,Help,login.html,
  5. STR-031,Version,login.html,
Notice the first line in the text file input where there is a space between the words Login and Name. Whenever my program reads it, it considers it as a new line, destroying the entire output. I've tried filtering the space by considering it as a char but it still produces the same output. What do you think should I do? Thank you.
Similar Threads
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
Aug 14th, 2007
0

Re: problems with reading in file

Additional question. Does ifstream reads a space as a beginning of a new line?
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
Aug 14th, 2007
0

Re: problems with reading in file

If you use getline() on line 64 you will avoid the space problem. getline() will read the entire line, then you can separate it into comma-separated tokens.

Line 61 is incorrect -- you should avoid using eof() like that because it will cause problems. Use getline() instead, something like this
C++ Syntax (Toggle Plain Text)
  1. while( getline(str,inFile) )
  2. {
  3. // blabla
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Aug 14th, 2007
0

Re: problems with reading in file

Click to Expand / Collapse  Quote originally posted by jaepi ...
Additional question. Does ifstream reads a space as a beginning of a new line?
No -- when you use the extraction operator >> the stream only reads up to the first space. That is why I suggest getline() in my previous post.
Last edited by Ancient Dragon; Aug 14th, 2007 at 12:20 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Aug 14th, 2007
0

Re: problems with reading in file

Thank you very much sir, it worked.
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Function with UnKnow Type
Next Thread in C++ Forum Timeline: Program to go from a character string to Binary, Octal, and Hexidecimal





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


Follow us on Twitter


© 2011 DaniWeb® LLC