By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

fscanf into an array

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.