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?

5
  • 5
    Awww that formatting... Commented Apr 5, 2013 at 6:27
  • try one thing... do this.. char *buff... and then printf("Data Read %s", buff);.. let me know if it works... I had solved my problem with this, not exactly but a little bit more... Commented Apr 5, 2013 at 6:28
  • stackoverflow.com/questions/410943/… Commented Apr 5, 2013 at 6:29
  • You need to check the return value from fread() before printing the data. Commented Apr 5, 2013 at 6:32
  • 1. you have to terminate buf with '\0'. 2. you have moved file pointer to the end and where is nothing to read any more. Commented Apr 5, 2013 at 6:34

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Or: fseek(fp, 0L, SEEK_SET);
@JonathanLeffler Yeah. That will also work. Maybe rewind(fp) calls fseek to do the work.
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;
}

Comments

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;

}

1 Comment

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.