fscanf problem

This is a discussion on fscanf problem within the C Programming forums, part of the General Programming Boards category; Hi im having a problem with fscanf i just dont undrestand how does it work this is the program ive ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    fscanf problem

    Hi im having a problem with fscanf i just dont undrestand how does it work this is the program ive done so far and it need to calculate the average of each student which is in the yafilea.txt and then display it can anyone help?
    i attach the files
    thanx
    Attached Files Attached Files

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Make your own homework?

    For help on fscanf() look here
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > char*course[60];
    Drop the * from all of these, so
    char course[60];

    > fgets(course, 60, fp);
    > fgets(modulea,30,fp);
    You need another fgets() to get rid of the blank line between these two records.

    > printf(course);
    Never pass an unknown string as a printf format string. Any % characters will ruin your day.
    Use fputs(), or printf("%s", course);

    > fscanf(marka,'%d',2);
    Something like
    int mark;
    fscanf( marka, "%d", &mark );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    i sked for help not solving the entire program thanx annyway
    is there any more specific information on this that i should use?!!

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    thanx salem im going to try this!!

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    i used
    fscanf(fp,marka,"%d",val1);
    printf("%d",val1);
    since i just wants to get for example 58 out of there but it gives me some crazy numbers
    and when i put
    fgets();
    it gives me an error

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    fscanf(fp,marka,"%d",val1);
    See this:
    Code:
    int fscanf(FILE *restrict stream, const char *restrict format, ... );
    In other words, why are you passing fscanf marka? Why does val1 lack an &?
    Code:
    fgets()
    Well, you have to pass it some arguments. http://cppreference.com/stdio/fgets.html

    A buffer, the number of characters in the buffer, and then the stream to read from (stdin). You might want to strip the newline too.

    Have a look at this: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<string.h>
    
    #ifdef WIN32
    #pragma warning( disable : 4244 4305 )
    #endif
    
    	
    
    double aggregate(float marka, float markb, float markc, float markd, float marke, float markf);
    
    //void main(void)
    /* Main should return */
    int main( void )
    {
    	
    	int average, val1, val2,val3, val4,val5;
    	char* course[60], modulea[30], moduleb[30], modulec[30];
    	char* moduled[30], modulee[30], *studenta [30];
    	char*marka [25];
    	char*studentb [30];
    	char*markb [25];
    	char*studentc [30];
    	char*markc [25];
    	char*studentd [30];
    	char*markd [25];
    	char*studente [30];
    	char*marke [25];
    	char*studentf [30];
    	char*markf [25];
    
    
    
    	FILE*fp;								//file pointer
    	static char fname[] = {"YAFILEA.txt"};  //file name
    	/*open the file for reading quit if not found */
    	fp=fopen("J:\\YAFILEA.txt","r");		
    
    	if( fp == NULL )
    	{
    		printf("cannot open &#37;s\n",fname);
    		exit(1);
    	}
    	
    	fgets(course, 60, fp);
    
    	/* need one more fgets to read the empty line in the file */
    	
    	/* You read all the modules which is right */
    	/* perhaps you could think of using some sort of loop rather than 
    	   calling the fgets so many times, which makes it more messy */
    
    	fgets(modulea,30,fp); 
    	fgets(moduleb,30,fp);
    	fgets(modulec,30,fp);
    	fgets(moduled,30,fp);
    	fgets(modulee,30,fp);
    	
    	fgets(studenta,30,fp);
    	fgets(marka,25,fp);
    
    	fgets(studentb,30,fp);
    	fgets(markb,25,fp);
    
    	fgets(studentc,30,fp);
    	fgets(markc,25,fp);
    
    	fgets(studentd,30,fp);
    	fgets(markd,25,fp);
    
    	fgets(studente,30,fp);
    	fgets(marke,25,fp);
    
    	fgets(studentf,30,fp);
    	fgets(markf,25,fp);
    
    	printf(course);
    
    	printf(modulea);
    	printf(moduleb);
    	printf(modulec);
    	printf(moduled);
    	printf(modulee);
    
    	printf(studenta);
        	printf(marka);
    
    	printf(studentb);
    	printf(markb);
    
    	printf(studentc);
    	printf(markc);
    
    	printf(studentd);
    	printf(markd);
    
    	printf(studente);
    	printf(marke);
    
    	printf(studentf);
    	printf(markf);
    
    	fclose(fp);	/*done close the file */
    	
    	fscanf(marka,'%d',2);  // fscanf(marks,&#168;%d %d %d %d %d&#168;, &mark1, &mark2, &mark3, &mark4, &mark5);
    	
    	getchar();
    	return 0;
    }
    		
    double aggregate(float marka, float markb, float markc, float markd, float marke, float markf)
    {	
    	double average;
    	average=(5);
    	
    	return average;
    }
    ssharish2005

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You missed a few things . . .
    1. iostream.h is old, non-standard C++. Not C.
    2. The printf() statements should not be like that. See Salem's post.
    3. &#168; is not the same as ", and what stream is the fscanf() reading from?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21