0

i really don't know how to use text file i i want to know the step by step procedure...
please thank you very much..

4
Contributors
4
Replies
7
Views
7 Years
Discussion Span
Last Post by Shankye
0
#include <fstream>

using std::ifstream;
char line[100];

void read(char* filename){

ifstream file;
file.open(filename);
getline(file,line);


}

int main(){

read(filename);
return 1;
}

Edited by alexchen: n/a

0

If you wish to open a file for reading. You could code the following:

#include <stdio.h>

main()
{

    FILE * myfileptr; // a pointer declared to the file stream.
    char sc; // a character which will be input from the file.
    // main code...
    myfileptr=fopen("fileiotst.c","r"); //open the file hopefully
    while((sc=getc(myfileptr))!=EOF) //get a char
    {

        if(sc)printf("%c",sc); //dump it to screen

        }
        printf("EOF\n");

}

Note: this procedure reads from the file character by character... if you wanted to read into a string variable you could build up a string from the characters, halting the build on the line end.

Or, if you knew the format of the file (ideally because you built it) you could use fscanf to read in the data. fscanf is the cousin of scanf which we are all familiar with.

Edited by Nick Evan: Added CODE-tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.