problems with reading in file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

problems with reading in file

 
0
  #1
Aug 13th, 2007
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:
  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:
  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.
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: problems with reading in file

 
0
  #2
Aug 14th, 2007
Additional question. Does ifstream reads a space as a beginning of a new line?
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problems with reading in file

 
0
  #3
Aug 14th, 2007
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
  1. while( getline(str,inFile) )
  2. {
  3. // blabla
  4. }
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: Aug 2005
Posts: 15,343
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problems with reading in file

 
0
  #4
Aug 14th, 2007
Originally Posted by jaepi View Post
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.
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: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: problems with reading in file

 
0
  #5
Aug 14th, 2007
Thank you very much sir, it worked.
Retreat!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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