Click here to Skip to main content
15,882,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

Pasting my simple code here, what I am trying to do is to copy all the contents of the file into char pointer to access it later. But dono where it is going wrong. Kindly advise. TIA.


int main()
        FILE *fp;
        char buf[500];
        char *result = new char[500];
                printf("\nfile opening error");
                return -1;
         char *buffer= (char*)malloc(500);
                printf("\nstrlen of buf %d",strlen(buffer));
                printf("\ncopied [%s] length= %d",result,strlen(result));
                printf("\nwhile result = %s",result);
        printf("\n outmsg= %s\n",result);
        return 0;
I am getting null values in result.

What I have tried:

C++ linux trying to store the file contents in char pointer to access it later.
Updated 13-Jan-23 12:42pm

You could
  • get the file size
  • allocate accordingly a buffer
  • read the whole file into the buffer

C++ provides several alternatives for performing such steps.
Share this answer
First off, you don't use buf at all - so dump that.
Second, strlen returns the number of characters that are before the first null character - so depending on your file content the printed data could be anything at all, but is unlikely to be anything particularly useful, since you print up to the first null char, then advance to a null and ... try to print that.

fgets only inserts a null when an end of line is reached - it doesn't do anything at all with data after that - so quite what you expect the second and final printfs of result to show you is anyone's guess.

And ... calling sizeof on any pointer returns the size of that pointer not the memory block it points to: for a 32 bits system that's 4, for a 64 bit system that's 8.

Have a think about what exactly you are trying to do, because that code looks like it was written without any real planning before leaping to the keyboard - and that's always a mistake! :D
Share this answer
There's no reason to use a loop if you're just dumping the whole file into memory anyway. That is unless you want to incrementally error out in the event of using too much memory, for some reason.

Copied from SO[^] :
#include <stdio.h>
#include <stdlib.h>

FILE *f = fopen("textfile.txt", "rb");
fseek(f, 0, SEEK_END);

long fsize = ftell(f);
fseek(f, 0, SEEK_SET);  // same as rewind(f);

char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);

string[fsize] = 0;
That being said though, doing that isn't always the best practice. It's fast but you're limited by memory. It's usually better to process your file line-by-line or chunk-by-chunk. In which case, you'll need that loop again.
Share this answer
Member 11815444 16-Jan-23 23:31pm    
Hi Jeremy Falcon, thanks it is working but how could i loop the same logic..TIA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900