| | |
problems with reading in file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
TEXT FILE INPUT:
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.
CODE:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <cctype> #include <string> #include <fstream> #include <iomanip> #include <stdlib.h> #include <windows.h> using namespace std; vector<string> split_String(const string& str) //reading csv file { vector<string> vStr; int i = 0; while (i != str.size()) { while (i != str.size() && str[i]==',' ){ i++; } int j = i; while (j != str.size() && str[j]!=',' ){ j++; } if (i != j) { vStr.push_back(str.substr(i, j - i)); i = j; } } return vStr; } int main() { vector<string> vCode; vector<string> vWord; vector<string> vFile; std::string my_File("sample.txt"); ifstream inFile(my_File.c_str()); std::string str1; const char *pStorage = 0; int arr = 0; if (!inFile) //if file cannot be read { cout << "Unable to open file"; exit(1); } else { while(!inFile.eof()) // reading the file line by line { inFile >> str1; pStorage = str1.data(); cout << "BEFORE = " << pStorage << endl; vector<string> vOutput = split_String(pStorage); for (int i = 0; i < vOutput.size(); i++) { if(i == 0) { vCode.push_back(vOutput[i]); cout << "CODE = "; } else if(i == 1) { vWord.push_back(vOutput[i]); cout << "WORD = "; } else if(i == 2) { vFile.push_back(vOutput[i]); cout << "FILE = "; } } cout << endl; } } inFile.close(); ofstream outfile("output.bat"); for(int x =0; x < vCode.size(); x++) { outfile<<"sed -e 's/"; outfile<<vCode[x]; outfile<<"/"; outfile<<vWord[x]; outfile<<"/g' "; outfile<<vFile[x]; outfile<<" > english/"; outfile<<vFile[x]; outfile<<"\n"; } outfile.close(); system("output.bat"); return 0; }
TEXT FILE INPUT:
C++ Syntax (Toggle Plain Text)
STR-000,Login Name,login.html, STR-001,Password,login.html, STR-029,Login,login.html, STR-030,Help,login.html, STR-031,Version,login.html,
Retreat!!!
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
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)
while( getline(str,inFile) ) { // blabla }
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.
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.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Class Error (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Quick TT_EOL question (Java)
- input from file into class (C++)
- CD-ROM has problems reading disc's (Storage)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Function with UnKnow Type
- Next Thread: Program to go from a character string to Binary, Octal, and Hexidecimal
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






