-
Help on my code please.
I am currently trying on this code, and I want the output to be the same as the input, but my code fails to do that. Please help me out.
Code:
/*
* A sample program to read-from and write-to files. Also shows how to
* send a file as a parameter and display error messages to standard
* error.
*/
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main () {
STUDENT stuArr[MAX_STUDENTS];
void rmNl ( char s[] );
void *fin, *fout;
void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]);
/* Open file to read input data */
if ( ( fin = fopen ( "grades.in", "r" ) ) == NULL ) {
fprintf ( stderr, "Could not open file grades.in\n" );
exit ( -1 );
}
/* Open file for writing output */
if ( ( fout = fopen ( "grades.out", "w" ) ) == NULL ) {
fprintf ( stderr, "Could not open file grades.out\n" );
fclose ( fin );
exit ( -1 );
}
grades_in(fin, fout, stuArr);
system ("PAUSE");
return 0;
}
void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]) {
int numItems = 0;
char buf[MAX_STUDENTS];
char fname[50], lname[50];
int h1, h2, h3, h4, h5, e1, e2, e3, id;
while( fgets ( buf, MAX_STUDENTS, fin ) != NULL ) {
rmNl ( buf );
if ( !lineWithBlanks ( buf ) )
if ( sscanf(buf, "%s %s", fname, lname) == 2 ) printf ("%s %s\n", fname, lname);
if ( sscanf(buf, "%d", &id) == 1 ) printf ("%d\n", id);
if ( sscanf(buf, "%d %d %d %d %d", &h1, &h2, &h3, &h4, &h5) == 5 ) printf ("%d %d %d %d %d\n", h1, h2, h3, h4, h5);
if ( sscanf(buf, "%d %d %d", &e1, &e2, &e3) == 3 ) printf ("%d %d %d\n", e1, e2, e3);
numItems++;
continue;
}
}
/*
* Function to remove new line character
*/
void rmNl ( char str[] )
{
char *ptr;
if ( ( ptr = strchr ( str, '\n' ) ) )
*ptr = '\0';
}
/*
* Check to see if line is an empty one with only spaces
* Returns 1 if line consists of blank spaces, 0 otherwise
*/
int lineWithBlanks ( char *s )
{
int result = 1, i;
for ( i = 0; i < strlen (s); i++ ) {
if ( s[i] != ' ' ) {
result = 0;
break;
}
}
return result;
}
my input data looks like this:
John Doe
1111
98 87 69 97 89
85 91 88
Bill Doe
2222
77 78 87 79 85
69 73 89
-
Well....from what I can see you've got a couple problems,
Code:
int main () {
STUDENT stuArr[MAX_STUDENTS]; STUDENT, MAX_STUDENT, are these in test.h? what are they?
void rmNl ( char s[] );function declarations should be outside of main
void *fin, *fout;void? should be FILE
void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]);function declaration again
/* Open file to read input data */
if ( ( fin = fopen ( "grades.in", "r" ) ) == NULL ) {grades.in?
fprintf ( stderr, "Could not open file grades.in\n" );
exit ( -1 );
}
/* Open file for writing output */
if ( ( fout = fopen ( "grades.out", "w" ) ) == NULL ) {grades.out?
fprintf ( stderr, "Could not open file grades.out\n" );
fclose ( fin );
exit ( -1 );
}
grades_in(fin, fout, stuArr);
system ("PAUSE");
return 0;
}
give that stuff a try then post again.
Hope that helps,
Crazed
-
Thank you for your help. Now I have a question: If I have an input data which looks like this:
Code:
John Doe
1111
98 87 69 97 89
85 91 88
Bill Doe
2222
77 78 87 79 85
69 73 89
how do I use fgets and sscanf with it? It seems my code doesn't scan to all the data...
-
This is the latest of my code editted. I just paste the fgets and sscanf part so it will be easier for you guys to examine it:
Code:
while( fgets ( buf, MAX_STUDENTS, datain ) != NULL ) {
rmNl ( buf );
if( !lineWithBlanks ( buf) ) {
if ( counter == 1 && (buf[0] != ' ')) {
if ( sscanf(buf, "%s %s", fname, lname) == 2 ) printf ("%s %s\n", fname, lname);
else printf("fail");
}
else if ( counter == 2 && (buf[0] != ' ')) {
if ( sscanf(buf, "%d", &id) == 1 ) printf ("%d\n", id);
else printf("fail");
}
else if ( counter == 3 && (buf[0] != ' ')) {
if ( sscanf(buf, "%d %d %d %d %d", &h1, &h2, &h3, &h4, &h5) == 5 ) printf ("%d %d %d %d %d\n", h1, h2, h3, h4, h5);
else printf("fail");
}
else if ( counter == 4 && (buf[0] != ' ')) {
if ( sscanf(buf, "%d %d %d", &e1, &e2, &e3) == 3 ) printf ("%d %d %d\n", e1, e2, e3);
else printf("fail");
}
counter++;
continue;
}
-
The output of the code is this:
John Doe
1111
98 87 69 97 89
85 91 88
It doesn't loop the second time, weird.
-
Get rid of the newline left hanging in the buffer. The second call to fgets() will get a blank string, which isn't what you want. FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392