简体   繁体   中英

Reading a text file in C

I am reading a text file and trying to display its contents on the console. Here is my code:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <fstream>

int main()
{
    FILE* fp=NULL;
    char buff[100];
    fp=fopen("myfile.txt","r");
    if(fp==NULL)
    {
        printf("Couldn't Open the File!!!\n");
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fread(buff,file_size,1,fp);
    printf("Data Read [%s]",buff);
    fclose(fp);
    return 0;
}

but only redundant data is being displayed on the console; could someone please point out my mistake?

You forgot to reset the file pointer to start after doing this.

fseek(fp, 0, SEEK_END);

Do this after finding size ( file_size ).

rewind (fp);

You need to seek back to the start of the file before reading:

int main()
{
    FILE* fp=NULL;
    char buff[100];
    fp=fopen("myfile.txt","r");
    if(fp==NULL)
    {
        printf("Couldn't Open the File!!!\n");
        exit(1);                     // <<< handle fopen failure
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);          // <<< seek to start of file
    fread(buff,file_size,1,fp);
    printf("Data Read [%s]",buff);
    fclose(fp);
    return 0;
}

Try it....

#include <stdio.h>
#include <stdlib.h>

void handle_line(char *line) {
printf("%s", line);
}

int main(int argc, char *argv[]) {
int size = 1024, pos;
int c;
char *buffer = (char *)malloc(size);

FILE *f = fopen("myfile.txt", "r");
if(f) {
  do { // read all lines in file
    pos = 0;
    do{ // read one line
      c = fgetc(f);
      if(c != EOF) buffer[pos++] = (char)c;
      if(pos >= size - 1) { // increase buffer length - leave room for 0
        size *=2;
        buffer = (char*)realloc(buffer, size);
      }
    }while(c != EOF && c != '\n');
    buffer[pos] = 0;
    // line is now in buffer
    handle_line(buffer);
  } while(c != EOF); 
  fclose(f);           
}
free(buffer);
return 0;

}

    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <fstream>

    int main()
    {
        FILE* fp=NULL;
        char *buff;                     //change array to pointer
        fp=fopen("myfile.txt","r");
        if(fp==NULL)
        {
            printf("Couldn't Open the File!!!\n");
        }
        fseek(fp, 0, SEEK_END);
        size_t file_size = ftell(fp);
        buff = malloc(file_size);      //allocating memory needed for reading file data
        fseek(fp,0,SEEK_SET);          //changing fp to point start of file data
        fread(buff,file_size,1,fp);
        printf("Data Read [%s]",buff);
        fclose(fp);
        return 0;
    }

having a buffer of 100 bytes to read a file is not a better idea as since the file size may be more than 100 bytes.

A better file io can be done by doing a fgets on the file, if its not a type of metadata that you wanted to read using the fread.

fgets in a while loop can be used to check whether its reached EOF or a feof call can be used to check the EOF.

a sample code listing of fgets can be like this:

 while (fgets(buf, len, fp)) {
      printf("%s", buf);
 }

or a sample that is used with fgets can be like this:

 while (fread(buf, len, 1, fp) >= 0) {
       printf("%s\n", buf);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM