Thread: Let's learn how to read!

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Angry Let's learn how to read!

    How could I make a program read a file line-by-line? Not string-by-sting; line-by-line.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User Null Shinji's Avatar
    Join Date
    Oct 2001
    Posts
    80

    Re: Let's learn how to read!

    Originally posted by CodeMonkey
    How could I make a program read a file line-by-line? Not string-by-sting; line-by-line.
    you wanna make a progam that reads a complete line into a string, i mean until end of line

    then you should use:

    Code:
    FILE *inp=fopen("input.dat","rt");
    char s[200][202];
    int k=0;
    while(!feof(inp)){
    fgets(s[k],inp,200);
    k++;
    }
    i'm not sure about the arguments order, i mean, i used fgets but dont know if thats the right order

    i used fgets(destination, sourcefile, maxlenght);
    you should check the help for getting the correct order
    Null Shinji The Sorcerer is here
    Evangelion Quotes:
    "If youre gonna do it, dont waste time. Otherwise, leave", Gendo
    "Release the final safety lock, Evangelion Unit One, Lift Off!!", Misato
    "Syncrograph has reversed, pulses are flowing back!!!", Maya
    streamload id= nullshinji icq= 12944337; E-M@IL= [email protected]; aim= mayeba
    msn= [email protected]

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Just use this...

    // reading a text file
    #include <iostream.h>
    #include <fstream.h> //input with files

    int main () {
    char buffer[256];
    ifstream examplefile ("example.txt"); //set file for reading.
    if (! examplefile.is_open()) //error when opening file
    { cout << "Error opening file"; exit (1); }

    while (! examplefile.eof() ) //while its not the end of file
    {
    examplefile.getline (buffer,100); //just like cin but from file
    cout << buffer << endl;
    }
    return 0;
    }
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 09:18 AM
  2. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  3. You have to learn C in order to learn C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-16-2004, 10:33 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM