fscanf into an array

T

totoro2468

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++;
}
 
R

Richard Heathfield

(e-mail address removed) 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?

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';
}
 
T

totoro2468

How do I use fgets?




Richard said:
(e-mail address removed) 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?

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)
 
K

Keith Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top