C Help - Open and Read a File

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Hi guys, could do with a hand here if possible.

Basically, I have a file called for example hello.txt, I simply want to read the contents of the file and store them into a declared variable. Probably a simple solution, but spent all day screaming at it.

Can anyone at least chuck me a hint as to what Im doing please.

Cheers
Stellios
 
Make a file pointer and a char buffer, assign the return of fopen to it. Check file pointer is not null. Use getc or fgetc to read characters out one by one and add them to the buffer (there are other methods but this is an easy way to do it). Use fclose to close the file.
 
somthing like

#include <stdio.h>

FILE* InputFile = fopen("c:\\yourfile.txt", "rb");
char* Data[];
fread(Data, 1, ncharacters, InputFile);
fclose(InputFile);
and then char* should have your data in.
 
Ah right, we werent far off then, just trying to do it in the wrong order.

Cheers guys, much appreciated. Cant believe we have spent the majority of the afternoon trying to write half a dozen lines of text.
 
somthing like

#include <stdio.h>

FILE* InputFile = fopen("c:\\yourfile.txt", "rb");
char* Data[];
fread(Data, 1, ncharacters, InputFile);
fclose(InputFile);
and then char* should have your data in.

My C is a little rusty, but isnt Data unitialised when you pass it to fread? Dont you need to malloc some space?
 
My C is a little rusty, but isnt Data unitialised when you pass it to fread? Dont you need to malloc some space?

It depends if we want the memory to be made on the stack or heap - so in this case, we dont mind it being on the stack so malloc is not required. However, a size must be given in the brackets or it will not compile. Also, malloc only sets aside space and returns a pointer to it, it does not initialise the memory - calloc would have to be used if you wanted initialised memory.
 
It depends if we want the memory to be made on the stack or heap - so in this case, we dont mind it being on the stack so malloc is not required. However, a size must be given in the brackets or it will not compile. Also, malloc only sets aside space and returns a pointer to it, it does not initialise the memory - calloc would have to be used if you wanted initialised memory.

Yup - its all coing back to me now. Though presumably initialisation isnt important, as fread will write to whatever space you give it.
 
You are probably right, i wrote that quickly and from a C++ perspective (i know there are differences)

You can do

char* data[] = "sometext";

or

char data[20];

or char* data = new char[20];

If you want to be pedantic you'd do

char* data = new char[20];
memset(data, 0, 20);

Lots of ways really, just keep swapping until it works ;)

Anyone who's tried to deal with MS string types in the dirextX SDK will know what i mean....
 
I expect you learnt a lot in the afternoon though so its all good :) Let us know if you need any more help.

Quite a lot yeah, especially considering Im terrible at programming :)

Basically its a small part of a program, we have to create a server and client system for a bus stop so that bus information can be transmitted from a control centre to a bus stop display.

1. Initialise connection between server and client
2. Client passes its client ID to server
3. Server reads corresponding IDs file to pull information, formats it correctly and passes it back to client
4. Client displays information on screen

Its proving a right pig. but we are getting there :)

Once thats done, then for the fun one, program a game of pong in assembly language :(
 
You can do

char* data[] = "sometext";

Actually this is a bad idea. The actual type of a string literal is 'const char *'. C compilers allow you to asssign this to a non const pointer for backwards compatibility.

However the compiler is completely free to allocate the memory for the string literal in read-only memory. Hence if you then pass the pointer to a function that attempts to write to the memory it will result in a segfault.

It *might* work, but that would be more down to the vagaries of a particular platform, and should not be relied on.
 
Actually this is a bad idea. The actual type of a string literal is 'const char *'. C compilers allow you to asssign this to a non const pointer for backwards compatibility.

However the compiler is completely free to allocate the memory for the string literal in read-only memory. Hence if you then pass the pointer to a function that attempts to write to the memory it will result in a segfault.

It *might* work, but that would be more down to the vagaries of a particular platform, and should not be relied on.

In fact it will give a compile error as an array of char * is being created which a string value cannot be given to.
 
Back
Top Bottom