Re: How to store the contents of a file into a variable





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

.



Relevant Pages

  • Re: Ordering BDS in Australia
    ... They made a mistake, they corrected ... > it before they charged your brother. ... sets on special and when you call the store they have none in stock. ... Won't call you when they get some in stock and have no rainchecks. ...
    (borland.public.delphi.non-technical)
  • Re: collection clone?
    ... I made a mistake. ... perhaps i need a mechanism to store the keys. ... > Dim i As Integer ... Prev by Date: ...
    (microsoft.public.vb.general.discussion)
  • Re: SS arrives in Australia/ Customer Service
    ... It is sort of natural not to look to ourselves first for the mistake. ... I called the store and spoke with the store manager who was very ... I can be a pretty good Sephora customer if they can keep items in stock. ... In the nail tech thread I think part of the problem is cultural. ...
    (alt.fashion)
  • Re: Argos selling Fable for £4.99
    ... In message, Alan writes ... Not sure if it's been posted here before, but I found one at a store 5 ... miles away and for £5 one can't really go wrong. ... If it is a pricing mistake then don't take advantage of other peoples mistakes. ...
    (uk.games.video.xbox)
  • Re: Object class is invalid for this container
    ... > get the error message "Object class is invalid for this container". ... > Obviously that's a mistake, because the class already exists in a class ... Prev by Date: ...
    (microsoft.public.fox.programmer.exchange)