I currently writing a program to read from file. I have succesffully done it, but i actually want to save the result in a variable, but i am not sure how t do it....
here is the code references from someone in the net.
pFile = fopen ("test.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
c = fgetc(pFile);
while (c != EOF)
{
printf("%c",c) ;
c = fgetc(pFile);
}
/* printf("%s", strings);
*//* i want to save the whole output to the char strings(buffer) but i am not sure how.....*/
fclose (pFile);
}
return 0;
}
Anyone can help, i really appreciate...., I am new in C language....
Thanks
pocs
bwkaz
08-29-2004, 01:04 PM
You can do this, but it will only work on Unix type systems (because it uses the Unix system call functions):
struct stat info;
int file_descriptor;
char *buffer;
file_descriptor = open("test.txt", O_RDONLY);
fstat(file_descriptor, &info);
buffer = malloc(info.st_size);
read(file_descriptor, buffer, info.st_size); Of course you'll need the headers for all these functions (open, fstat, malloc, and read), but you can find those out from their manpages (yes, most C functions have manpages -- at least system calls like open, read, and fstat, and C library calls like malloc, do).
No matter how you do it (whether you try to append to your "strings" buffer, or you do it this way), you will NEED to use malloc() to allocate your buffer. If you don't, you will eventually run into a file that's bigger than your buffer, which (if you don't use functions that check the length of what they read into the buffer before blindly storing it there) will cause a buffer overrun. If that overrun is accidentally triggered, your program will crash. If it's deliberately triggered, it turns into a security hole.
If you do use a function that checks against the length of the buffer, then it just won't read in the whole file (it'll only read in up to the length of the buffer). read() is one of those functions, but we're giving it the length of the file (info.st_size) as the length of the buffer -- and we've also malloc()'ed that many bytes for the buffer, so it is actually that big.
pFile = fopen ("test.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
int i = 0;
c = fgetc(pFile);
while (c != EOF)
{
printf("%c",c) ;
strings[i] = c;
i++;
c = fgetc(pFile);
}
/* printf("%s", strings);
*//* i want to save the whole output to the char strings(buffer) but i am not sure how.....*/
fclose (pFile);
}
return 0;
}
i test the code and try to compile,
but compliation error.
:buffer = malloc(info.st_size);
warning:assignemnt makes pointer from integer without a cast
Thanks
Energon
08-30-2004, 01:52 AM
Actually, here's a standard way to do it:
char* buffer=NULL;
long len=0;
FILE* f = fopen("test.txt", "r");
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = (char*)calloc(len+1, 1);
fgets(buffer, len, f);
/* do something with the buffer */
free(buffer);
buffer = NULL;
That opens the file, gets the length of it by going to the end and finding the position (in bytes), allocates the appropriate amount of space (+1 for the terminating NULL), reads the entire file into the buffer, and finally frees the buffer after you use it. Error checking omitted for brevity.
pocs
08-30-2004, 02:48 AM
Hi Energon,
I have test your code, it works fine.
but if i do
printf("%s", buffer);
it will only print the first line of my file.....
how to print the whole thing in file?.
I am so noob in C.....:(
stoe
08-30-2004, 11:26 AM
Well, you could just loop over the entire array and print each char one by one ...
replace
printf("%s", buffer);
with
for(i=0; i <= len; i++)
printf("%c", buffer[i]);
perhaps not the most efficient way, but it should do the trick.
pocs
08-30-2004, 11:34 AM
ooo ic,
so how about if i do it this way?
FILE * pFile;
char strings [255];
char c;
pFile = fopen ("test.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
int i = 0;
c = fgetc(pFile);
while (c != EOF)
{
printf("%c",c) ;
strings[i] = c;
i++;
c = fgetc(pFile);
}
printf("%s", strings);
Will this cause any problem for the future code?
so this is a very bad way to code in C??
BTW: how to make the piece of code to ident properly in the forum?
tecknophreak
08-30-2004, 12:44 PM
hit the # button in the vB Code box
bwkaz
08-30-2004, 08:52 PM
Or manually insert before your code, and after it (but to prevent the forum from interpreting those, I had to insert an opening italics and closing italics in the middle of those tags, so don't quote my post to find out how I did it, just copy them right from the post as you see it ;)).
I should have said something when I edited your very first post to include those tags, sorry about that. If you want, you can still edit your other posts to include them.
If you do it the way you've posted, the one thing you will need to change is, you'll have to make your "c" variable into an int. fgetc() returns an int, not a char.
The reason it returns an int and not a char is because the char version of EOF is not the same bit value as the int version of EOF. The way you have your code, I can put a single-byte value of 255 into a file, and your program will compare that byte (chars are always one byte) equal to EOF (which is an int, with value -1). If you make c an int, then it will get the value 255 instead of -1 at that byte, and it will compare not equal to EOF.
However, when you put c into your "strings" array, you will have to manually cast it back to a char to avoid a compiler warning. You will also want to cast it to a char to avoid the possibility of a warning in your printf() function call (because you have a character format, but an int argument -- I don't think GCC warns about this, but it might, and I believe some other compilers definitely do).
And definitely move away from char strings[255]. You will create a buffer overrun as soon as you try to read a file that's 257 bytes long, or (worse yet) 4000 bytes long, or in fact any other length bigger than 256 bytes. I pretty much completely forgot about Energon's fseek / ftell functions; use those instead. (You have a small chance of somebody else changing the file out from under you, but if you ensure you only read that many bytes, the worst that can happen is you get an EOF error, or you don't read the whole file; not a huge deal.)
pocs
08-31-2004, 01:58 PM
oh ic, so i have to type cast, no wonder when i use the funtion and it print some strange character at the end of the file.
so this will be correct:
#include <stdio.h>
int main()
{
FILE * pFile;
char strings [4096];
int c;
pFile = fopen ("test.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
int i = 0;
c = fgetc(pFile);
while (c != EOF)
{
/* printf("%c",(char)c) ;
*/ strings[i] = ((char)c);
i++;
c = fgetc(pFile);
}
printf("%s", strings);
fclose (pFile);
}
return 0;
}
but i don't actually need to use a for loop to do the printf. :)
pocs
08-31-2004, 02:08 PM
ok, Thanks guys,
but i have one more question....:(
I am actually trying to write a program for client and server,
For this code, i understand it will read a line by line, but then i am trying to append every each of the line to my buffer temp1 using snprinf or sprinf, it won't work, because everytime i test it with the last line "----------testing mailbox fileope:%s\n", line);" it will only print the last line, I am not sure why....
as for te Writen, it is just a funtion to write the message of line buffer to the client.
thanks
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.