Advertisement

reading from a file

Started by May 16, 2002 07:56 PM
19 comments, last by gamefr 22 years, 6 months ago
how can you let your program read from the second line or third or after a specific character like:

name = Pipo
/*secondline*/health = 999999

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima? Thought So...
If you want to show exactly what is in the text file try:

infile.get();
cout.put();

in a while loop.
Don't forget to include iomanip
Actually I'm not sure you need iomanip for this, I don't really remember!?!


______________________________
[Free Books!] [Reference and Tutorials]
[For Beginners] [IGDA]
[OpenGL Tutorials] [Sourceforge]
[DirectX Tutorials] [Gamasutra]

[edited by - catfoodgood on May 17, 2002 6:18:49 PM]

[edited by - catfoodgood on May 17, 2002 6:20:31 PM]
Advertisement
quote: Original post by Pipo DeClown
how can you let your program read from the second line or third or after a specific character...


Read chars by one and skip until you hit your 'specific character':

      // assuming infile is positioned on the beginning of linewhile (infile.get() != '=')    // do nothing    ;// read whatever it is after =    int some_var;    infile >> some_var;    

If you want to seek to a certain line, you can do this:

  // line_num is the number of line that you wantfor (int i = 1; i < line_num; ++i){    whie (infile.get() != '\n')        // do nothing        ;}      

For robust code, you should check for eof using infile.eof() before attempting to read each character.


[edited by - IndirectX on May 17, 2002 6:24:48 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
For Pipo''s example I would read words, keep reading chars into a string until the word matches a tag, then return the string as a word. That way the actual line numbering isn''t important, as you just search for tag "Name =" and return the remainder of the line as a parameter, you could go further and use spaces or comma''s to seperate multiple parameters on the same line, or use specific line terminaters like ";".

,Jay
could you tell me how?

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima? Thought So...
I wrote a long explanation yesterday and then IE crashed (again), I hate windows with a passion the likes of which is seldom seen when that happens .

This is what I''d do (psudo-code)

(1) Read in the file to a string
(2) Use the substr (?) function to find a substring (Tag).
(3) Advance the position indicater (I) returned by substr by the length of Tag.
(4) Return a string (strParam) comprised of the start point I to the end point Ascii 13 (carrage return) + Ascii 10 (forget what but its used in notepad type text at line-end). Or of couse you could use a line terminating symbol like '';'' for your endpoint.
(5) Use Tag to decide if parsing into multiple parameters is necessary, then convert into int/float etc. using atoi and similar functions.

This is more C than C++ as i''m not very familer with the stream operators and string class, but it will work effectivly.

,Jay
Advertisement
sorry but could you tell me how, give me an example?

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima? Thought So...
Printing a file to cout in one go:
#include <iostream>#include <fstream>using namespace std;ifstream ifs( "Ogre.txt" );cout << ifs.rdbuf(); 


Loading a file in a string (or in a vector), then printing it :
#include <iostream>#include <fstream>#include <iterator> // for istream_iterator#include <string>using namespace std;ifstream ifs( "Ogre.txt" );string str( istream_iterator<char>(ifs),            istream_iterator<char>() );cout << str; 


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I''d try reading the file in binary mode and then just printing out directly to the display...
Don''t input routines automatically eat whitespace? Is your problem that it''s not showing up right?

Gamer-Insight.com

This topic is closed to new replies.

Advertisement