473,411 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

fscanf into an array

Hi, I'm a complete NOOB.
How do I get fscanf to copy into an array? I also need to use malloc,
but where do I put it in my file?
__________________________________________________ _____
FILE *ifp;

char c;
int y = 0;

txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;

printf ("Enter the name of the file to analyze : ");
gets (filename);
printf ("\n");
ifp = fopen(filename, "r");

if (ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while(fscanf (ifp, "%c", &c) != EOF)
{
txtFile[y] = c;
y++;
}

Nov 5 '06 #1
3 6665
to********@gmail.com said:
How do I get fscanf to copy into an array?
One element at a time.
I also need to use malloc,
but where do I put it in my file?
At the point where you first need the memory. Be sure to #include <stdlib.h>
for the malloc prototype.
FILE *ifp;

char c;
int y = 0;

txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;
txtFile = malloc(FILELENGTH * sizeof *txtfile);
if(txtFile != NULL)
{
>
printf ("Enter the name of the file to analyze : ");
gets (filename);
Don't use gets. It's a buffer overflow waiting to happen. Instead, use
fgets, find the newline using strchr, and overwrite it with '\0' if it's
found. (If it's not found, you probably don't want to accept the filename
anyway.)
printf ("\n");
ifp = fopen(filename, "r");

if (ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while(fscanf (ifp, "%c", &c) != EOF)
I recommend == 1 rather than != EOF. fscanf returns the number of fields
successfully converted, and you're expecting one field, so that's what you
should test for.
{
txtFile[y] = c;
y++;
}
This is fine provided y never equals or exceeds the array bound. But, to
answer your question(!), you can instead do this:

y = 0;
while(y < FILELENGTH && fscanf(ifp, "%c", &txtFile[0]) == 1)
{
y++;
}

if(y == FILELENGTH)
{
you ran out of buffer, but at least no harm was done.
}

If you plan to use txtFile as a string, don't forget to null-terminate it,
which entails leaving space for a null terminator:

y = 0;
while(y < FILELENGTH - 1 && fscanf(ifp, "%c", &txtFile[0]) == 1)
{
y++;
}

if(y == FILELENGTH - 1)
{
you ran out of buffer, but at least no harm was done.
}
else
{
txtFile[y] = '\0';
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 5 '06 #2
How do I use fgets?


Richard Heathfield wrote:
to********@gmail.com said:
How do I get fscanf to copy into an array?

One element at a time.
I also need to use malloc,
but where do I put it in my file?

At the point where you first need the memory. Be sure to #include <stdlib.h>
for the malloc prototype.
FILE *ifp;

char c;
int y = 0;

txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;

txtFile = malloc(FILELENGTH * sizeof *txtfile);
if(txtFile != NULL)
{

printf ("Enter the name of the file to analyze : ");
gets (filename);

Don't use gets. It's a buffer overflow waiting to happen. Instead, use
fgets, find the newline using strchr, and overwrite it with '\0' if it's
found. (If it's not found, you probably don't want to accept the filename
anyway.)
printf ("\n");
ifp = fopen(filename, "r");

if (ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while(fscanf (ifp, "%c", &c) != EOF)

I recommend == 1 rather than != EOF. fscanf returns the number of fields
successfully converted, and you're expecting one field, so that's what you
should test for.
{
txtFile[y] = c;
y++;
}

This is fine provided y never equals or exceeds the array bound. But, to
answer your question(!), you can instead do this:

y = 0;
while(y < FILELENGTH && fscanf(ifp, "%c", &txtFile[0]) == 1)
{
y++;
}

if(y == FILELENGTH)
{
you ran out of buffer, but at least no harm was done.
}

If you plan to use txtFile as a string, don't forget to null-terminate it,
which entails leaving space for a null terminator:

y = 0;
while(y < FILELENGTH - 1 && fscanf(ifp, "%c", &txtFile[0]) == 1)
{
y++;
}

if(y == FILELENGTH - 1)
{
you ran out of buffer, but at least no harm was done.
}
else
{
txtFile[y] = '\0';
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 5 '06 #3
to********@gmail.com writes:
How do I use fgets?
Please don't top-post. Read the following:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

Please don't quote the entire article to which you're replying; quote
only the portions that are relevant to your followup.

How do you use fgets()? You start by reading its description in
whatever textbook or tutorial you're using to learn C, or by reading
the system documentation ("man fgets" on some systems) to find out how
it's used.

We can answer questions here, but we can't teach you the entire
language. The most valuable think you can learn is how to find out
these things for yourself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
5
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52...
9
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the...
6
by: InuY4sha | last post by:
Hi, I hope to be not off topic.. I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC address) I use fscanf like this fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X", ptr, ptr+1, ptr+2,...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
2
by: wilco | last post by:
Hi, im filling a 2D array using fscanf and reading from a csv file. the problem is the file im reading from has some blank spaces which causes any information following to be output as an error. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.