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..

Recommended Answers

All 4 Replies

#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;
}

Its C forum ..

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.