Ashely
2013-06-27 20:25:19 UTC
1. Create a data file called friends.dat using any text-based editor and enter at least three records storing your friends’ first and last names. Make sure that each field in the record is separated by a white space.
2.Using the friends.dat file from challenge number one, build another program that uses the fscanf() function for reading each record and printing field information to standard output until the end-of-file is reached. Include an error-handling routine that notifies the user of any system errors and exits the program.
Here is the code that I have so far:
#include
#include
#include
int main (void)
{
FILE *inFile;
char tmp[10];
size_t stringLength;
int output;
int count;
count = 1;
inFile = fopen (“\friends.dat”, “r”);
if (!inFile)
{
fprintf (stderr, “could not open file %s for read!\n”, “friends.dat”);
exit (EXIT_FAILURE);
}
while (1)
{
char *tmpString;
output = fscanf (inFile, “%s”, tmp);
stringLength = strlen (&tmp);
tmpString = (char *) calloc stringLength, sizeof (char));
strcpy (tmpString, tmp);
if (count %2 == 0)
fprintf (stdout, “%s\n”, tmpString);
else
fprintf (stout, “%s\n”, tmpString);
if (output == EOF)
break;
count ++;
}
Here is what is in my friend.dat file:
Robert Mandino, 24, #1122332, Floor Echo -1
Sarina Larso, 25, #2211331, Floor Echo -2
Chris Murdock, 29, #3311223, Floor Echo -3
Here are the errors I keep recieving:
C:\Users\student\Desktop\Untitled1.c In function `main':
12 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
12 C:\Users\student\Desktop\Untitled1.c stray '\' in program
12 C:\Users\student\Desktop\Untitled1.c `friends' undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in.)
12 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
12 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
12 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
12 C:\Users\student\Desktop\Untitled1.c `r' undeclared (first use in this function)
15 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
15 C:\Users\student\Desktop\Untitled1.c `could' undeclared (first use in this function)
15 C:\Users\student\Desktop\Untitled1.c syntax error before "not"
15 C:\Users\student\Desktop\Untitled1.c stray '\' in program
15 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
15 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
15 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
21 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
21 C:\Users\student\Desktop\Untitled1.c syntax error before '%' token
21 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
22 C:\Users\student\Desktop\Untitled1.c [Warning] passing arg 1 of `strlen' from incompatible pointer type
23 C:\Users\student\Desktop\Untitled1.c syntax error before "stringLength"
26 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
26 C:\Users\student\Desktop\Untitled1.c syntax error before '%' token
26 C:\Users\student\Desktop\Untitled1.c stray '\' in program
26 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
28 C:\Users\student\Desktop\Untitled1.c stray '\147' in program
28 C:\Users\student\Desktop\Untitled1.c stray '\' in program
28 C:\Users\student\Desktop\Untitled1.c stray '\148' in program
32 C:\Users\student\Desktop\Untitled1.c syntax error at end of input