Re: How to store the contents of a file into a variable
- From: Eric Sosman <eric.sosman@xxxxxxx>
- Date: Thu, 28 Apr 2005 14:25:34 -0400
francescomoi@xxxxxxxxxx wrote:
> Hi.
>
> I want to store the contents of a file into a variable:
> ------------
> char file_name[] = "/home/user/foo.txt";
> FILE *my_file;
> my_file = fopen(file_name, "r");
>
> char *file_data;
> fscanf(my_file, file_data);
>
> printf("%s\n", file_name);
> printf("%s\n", file_data);
> -------------------------
>
> But I get:
> ------------------------
> /home/user/foo.txt
> (null)
> -------------------------
>
> Of course, my 'foo.txt' file is not empty. Whay am I doing wrong? Thx.
Several things. One mistake is covered by Question 7.1
(see also Questions 7.2 and 7.3) in the comp.lang.c Frequently
Asked Questions list
http://www.eskimo.com/~scs/C-faq/top.html
Another of your mistakes doesn't seem to be covered in
the FAQ. Point to ponder: Where is the "scan pattern" or
"scan format" in your fscanf() call?
--
Eric.Sosman@xxxxxxx
.
- References:
- How to store the contents of a file into a variable
- From: francescomoi
- How to store the contents of a file into a variable
- Prev by Date: Re: How is strlen implemented?
- Next by Date: Re: Value of EOF
- Previous by thread: How to store the contents of a file into a variable
- Next by thread: Re: How to store the contents of a file into a variable
- Index(es):
Relevant Pages
|