Skip to main content
GameDev.net gamedev.net
🔒 Locked

File reading....

Started by demonrealms Jun 1, 2003 at 3:07 PM 2 replies 500+ views
Original Post
demonrealms
demonrealms
How do you make it so(using visual c++ 6.0) your program will read a fiel line by line? I can get it to read one line and that''s it. Heres my code:
char system[180];
ifstream system_01( "tower.data" );
system_01 >> system;
system_01.close();
cout << system << endl;
how do I get it to read line by line? And if you could, how do you get it to find and read a certian line, then stop reading by a * symbol?
Lektrix
Lektrix
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    ...

    std::ifstream system_01("tower.data");

    std::string line;
    std::getline(system_01, line);

    ...

    std::cout << line << std::endl;

    ...

    return 0;
}

getline()
takes the form of:

std::getline(std::ostream& os, std::string& str, char delim = '\n');

So, in my example, it will read from system_01 until it reaches the last paramter (defaulted to the newline character ('\n')), placing it in line. So, for stopping at the '*' symbol, you would do something like:

std::getline(system_01, line, '*');


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on June 1, 2003 6:03:54 PM]
demonrealms
demonrealms
It only oputs one line though, why?
demonrealms
demonrealms
Never mind, it works except one problem...
when I put the * it only puts a space in beween the lines.
then it continues going.
std::ifstream system_01("tower.txt");
while(!system_01.eof())
{
std::string line;
std::getline(system_01, line, '*');
std::cout << line << std::endl;
}

Anyone know why?

[edited by - demonrealms on June 1, 2003 8:16:56 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.