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. 2 2206 fr**********@eu rope.com 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?
-- Er*********@sun .com fr**********@eu rope.com wrote: Hi.
I want to store the contents of a file into a variable: ------------ char file_name[] = "/home/user/foo.txt";
You could use:
const char * file_name = "/home/user/foo.txt"
FILE *my_file; my_file = fopen(file_name , "r");
I suggest you test "my_file" for NULL.
If it is, there was a problem opening the file.
char *file_data; fscanf(my_file, file_data);
You have declared a pointer, "file_data" , but
not assigned it to point to anything.
You are also missing some {input} format
specifiers. See your reference book.
Perhaps you need:
const unsigned int MAX_DATA_SIZE = 256;
char * file_data;
file_data = malloc(MAX_DATA _SIZE);
fscanf(my_file, "%s", file_data);
Or this might work also:
const unsigned int MAX_DATA_SIZE = 1024;
char * file_data;
file_data = malloc(MAX_DATA _SIZE);
fread(file_data , 1, MAX_DATA_SIZE, my_file); 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.
Remember:
1. Always check the return values of:
malloc
fopen
fscanf
fread
2. The C language does not have a dynamic string
type. You will have to know how much to allocate
at first, then maybe reallocate again.
3. Pointers, when declared, don't point anywhere
useful. You will have to make them point to
something before you use them.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq: http://www.comeaucomputing.com/learn/faq/
Other sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: vishal |
last post by:
hi
vishal here. can anyone help me that how can i store the global
variables for my web site. suppose i want to store the no. of users
visited my site then what strategy should i use ?????
can i implement some lock mechanism so that this global variable is not
accessed by more than one application at some time which may create
some confusion.
|
by: Bart Plessers \(artabel\) |
last post by:
Hello,
Currently developping a site, where I use some session variables.
The site is located at my home computer.
Because I have a dynamic IP adress, I use no-ip (www.no-ip.org) to have my
own custom domain name (pvo.no-ip.org)
My ISP blocks port 80, so website runs at port 4040
The service no-ip offers "Mask / Cloaking Options": every request to my
domain is "wrapped in a frame", so end user can only see one URL in browser.
|
by: F. Da Costa |
last post by:
Hi,
Does anybody know why IE5+ does *not* honour array objects (like a table)
across a session?
Example:
Frame A contains a var tableVar which is set via form Frame B (on init)
using top.A.tableVar = document.getElementById("someTable");
As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
|
by: hoopsho |
last post by:
Hi Everyone,
I am trying to write a program that does a few things very fast
and with efficient use of memory...
a) I need to parse a space-delimited file that is really large,
upwards fo a million lines.
b) I need to store the contents into a unique hash.
c) I need to then sort the data on a specific field.
d) I need to pull out certain fields and report them to the user.
|
by: moondaddy |
last post by:
I'm caching a dataset in an asp.net session variable to hold a user's data.
one data item I need to store is an image the user uploaded. My problem is
that I don't know how to get the image into the dataset because I don't know
what datatype to set the dataset column and then set this image to. I saw
an example where someone defended a datatable in the global class and
defined the column as an object, however, when I define a strongly...
| |
by: Mike9900 |
last post by:
Hello,
I would like to store application expiration date in a file and store that
file in a secure place, so the application can access the file for all the
users on that computer.
IsolatedStorage is a good technique but it is for the each user only and is
not machine level. Registry is not good because the user may not have access
permission. Application directory is not good because the file could be
deleted and so the...
|
by: John Kotuby |
last post by:
Hi all,
I am using a 3rd party program in a VS2005 web project. The tool takes as
input a string containing HTML and converts it to RTF.
I have been creating a page by dynamically loading UserControls and then
sending the page to the browser.
Now I want to write the contents of the the page to a string variable,
convert that variable to the RTF and send that to the browser and I am stuck
on the "simple" part of getting the contents of...
|
by: ovisvana |
last post by:
Hi,
Iam using javascript to submit a form to a php page on a remote server which is returning me another php page. What I would like is to store the contents of the php page in a variable in my program. However, the browser is popping up a dialog asking me if I would like to save the returned file. How do I achieve what I want. The same php Iam calling is being used by the remote server and they don't have this problem.
cheers,
prakash...
|
by: Dennis |
last post by:
Hi, I have a text file that contents a list of email addresses like
this:
"foo@yahoo.com"
"tom@hotmail.com"
"jerry@gmail.com"
"tommy@apple.com"
I like to
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |