Thread: Print from file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question Print from file

    I'm trying to get this code to print from a file data.txt which is in my c folder... it's compiling without errors or warnings, anyone see what could be wrong? As soon as I run the program prints nothing and closes (return 0). Doesn't even show me the menu I made.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void printStudents( struct datarec *a, int k);
    
    struct datarec {
    char name[15];
    char ssn[12];
    char gender;
    double a, q, m, p, f;
    char grade;
    };
    
    int main()
    {
    	struct datarec temp;
    	struct datarec x[20];
    	
    	int j; // used in menu
    	int k; // used in while loop
    	
    	FILE *fp; // file pointer
    	fp = fopen("c:\\data.txt","r");
    	
    	k = 0;
    	
    	while( fscanf (fp,"%15s %11s %c %lf %lf %lf %lf", 
    		temp.name, 
    		temp.ssn, 
    		&temp.gender, 
    		&temp.a, 
    		&temp.q,
    		&temp.m, 
    		&temp.p, 
    		&temp.f )!= EOF ); {
    	
    		temp.name[14]='\0'; 
    		temp.ssn[11]='\0';
    		x[++k] = temp;
    		
    	}
    	
    	do {
    	
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    		printf("@                                              @");
    		printf("@ 1. Print list of students                    @");
    		printf("@ 2. Print list of students with letter grade  @");
    		printf("@ 3. Print list of students with an A grade    @");
    		printf("@ 4. Print list of students with an F grade    @");
    		printf("@ 5. Print list of students sorted by name     @");
    		printf("@ 6. Add student to the class list             @");
    		printf("@ 7. Delete student from class list            @");
    		printf("@ 8. Exit                                      @");
    		printf("@                                              @");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    		
    		scanf("%d", &j);
    		
    		if (j == 1) { printStudents(x, k); }
    	
    	} while (j != 8);
    	
    	return 0;
    
    }
    
    void printStudents(struct datarec *a, int k)
    {
    	int i;
    	
    	for(i=0; i < k; ++i) {
    		printf( "Name:%-15s SSN:%-11s Gender:%c %4.1f %4.1f %4.1f %4.1f\n",
    		a[i].name, 
    		a[i].ssn, 
    		a[i].gender, 
    		a[i].a,
    		a[i].q, 
    		a[i].m, 
    		a[i].p, 
    		a[i].f);
    	}
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    use a getch(); before the return 0. getch waits for a character from the user before going to the next line. Without getch(). the program reaches return 0 and hence terminates . (It actually prints on screen but its speed is too large to be visible).

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    nope, that's not it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be careful about the difference between "this program has no warnings because it's a good program" and "this program has no warnings because I turned the warnings off on my compiler":
    Code:
    temp.c:88: warning: too many arguments for format
    I'm not sure why I didn't get a warning for the fscanf line, but it too has eight things it wants to read and only 7 conversion specifiers.

    But your program runs, presents the "menu", etc. just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM