0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aayman Khalid
  • 446
  • 3
  • 12
  • 24

5 Answers5

4

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);
dejavu
  • 3,236
  • 7
  • 35
  • 60
3

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;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
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;

}

Chirag Pipaliya
  • 1,281
  • 12
  • 20
  • That's a lot of code. You check the `fopen()` result and don't use it if the open failed — which is good. Theoretically, you should check the `malloc()`. You should not use `buffer = realloc(buffer, size);` because if the `realloc()` fails, you've lost the only pointer to the allocated memory and you have therefore leaked whatever was allocated before. I think I'd be using `fgets()` or POSIX `getline()` rather than character-by-character input. – Jonathan Leffler Apr 05 '13 at 06:36
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;
    }
mahe5_36
  • 7
  • 1
  • 4
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);
 }
John
  • 449
  • 5
  • 12