| | |
reading from a text file: not line by line but values seperated by a space
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
all i can do is get each line at a time....
how can i read it to only read till the space....
plz help.... heres my lil bit of code
plz i know i can use this aswell for line by line
thanx in advanced
how can i read it to only read till the space....
plz help.... heres my lil bit of code
C++ Syntax (Toggle Plain Text)
while (getline(iofile,line)) { cout << line << endl; }
plz i know i can use this aswell for line by line
C++ Syntax (Toggle Plain Text)
while (!iofile.fail()) { getline(iofile,line) cout << line << endl; }
thanx in advanced
Last edited by phoenix911; May 25th, 2009 at 7:41 am.
C++ Syntax (Toggle Plain Text)
while (getline(iofile,line)) { cout << line << endl; }
Your code is on the right track. Check out the
getline function's prototypes here: getline - C++ Reference. The file structure you have described is known as a "Space-delimited File". In your getline function, specify the delim parameter. (If you have followed the link, you will know what to pass as this third parameter).Hope this helps!
NOTE: This type of question has been asked many times before, in fact some very visible in the main Index of this forum! Try to at least search these forums/or the web before posting your question. A LOT of information is available on the net on this topic.
Last edited by amrith92; May 25th, 2009 at 7:52 am.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
thanx...will check it out right away...
but b4 i do... im using something like this now
will this help???
but b4 i do... im using something like this now
C++ Syntax (Toggle Plain Text)
iofile >> data1 >> data2 >> data3 >> data4;
will this help???
•
•
•
•
thanx...will check it out right away...
but b4 i do... im using something like this now
C++ Syntax (Toggle Plain Text)
iofile >> data1 >> data2 >> data3 >> data4;
will this help???
getline in the first place. Stick with your original code. "C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
so far i got this
now is what im trying to do right? read from the text file and put it into an array?
C++ Syntax (Toggle Plain Text)
fighter fight[80]; // this is a class with an array size //the class looks as follows class fighter { string name, special; int str, agi, spd; public: void set(string n, string s, int st, int ag, int sp) { name = n; special = s; str = st; agi = ag; spd = sp; } // end of class while (getline(iofile,line)) { iofile >> name >> special >> str >> agi >> spd; fight[n].set(name, special, str, agi, spd); n++; }
now is what im trying to do right? read from the text file and put it into an array?
Last edited by phoenix911; May 25th, 2009 at 8:10 am.
Line by line, word by word:
Think
...
C++ Syntax (Toggle Plain Text)
void WordByWord(const char* filename) { if (!filename) return; ifstream file(filename); string line, word; for (int lineno = 1; getline(file,line); lineno++) { cout << "Line #" << lineno << ":\n"; istringstream words(line); while (words >> word) cout << word << '\n'; } }
... Did you actually go through the links I gave, and the clue I provided in my previous posts? If so, did you not understand it? and no, your code still will read your file line-by-line.
> read from the text file and put it into an array?
A similar(if not exactly the same) question has been asked at least twice in the past week in this same forum. Search for answers first, and iff you did not understand it, or else your problem hasn't been addressed to yet, then ask the question.
> (To ArkM): Nice Code!
> read from the text file and put it into an array?
A similar(if not exactly the same) question has been asked at least twice in the past week in this same forum. Search for answers first, and iff you did not understand it, or else your problem hasn't been addressed to yet, then ask the question.
> (To ArkM): Nice Code!
Last edited by amrith92; May 25th, 2009 at 8:19 am.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
yes i did read ur post... and actually what i posted is working talking bout the it actually does what i want...
only prob i have now is storing it in2 an class array :/
C++ Syntax (Toggle Plain Text)
iofile >> data1 >> data2 >> data3 >> data4;
only prob i have now is storing it in2 an class array :/
Last edited by phoenix911; May 25th, 2009 at 8:22 am.
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
heres my entire class
what im trying to do here... is display the class array
C++ Syntax (Toggle Plain Text)
class fighter { string name, special; // when i compile it says error is here int str, agi, spd; public: void set(string n, string s, int st, int ag, int sp) { name = n; special = s; str = st; agi = ag; spd = sp; } };
what im trying to do here... is display the class array
C++ Syntax (Toggle Plain Text)
for (n = 0; n < 80; n++) { cout << fight[n].name << endl; // and this is part of the error }
Last edited by phoenix911; May 25th, 2009 at 9:01 am.
![]() |
Similar Threads
- Reading in a text file -- efficiently (Java)
- VERY BASIC C++ >> reading from a text file (C++)
- reading a text file to a remote PC (ASP.NET)
- Read text file contents per line (C++)
- c++ reading text file (C++)
- Read a specific line from a text file - how to ? (Java)
- Reading a text file (C++)
- Reading in a text file string is not complete (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: Same problem as SetTimer
- Next Thread: Visual Studio 2005 Icon Problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






