Read, Write on files.. counting lines.. stuff like that

This is a discussion on Read, Write on files.. counting lines.. stuff like that within the C Programming forums, part of the General Programming Boards category; Hello everybody.. I'm an italian boy, who has an exam right tomorrow.. I'm trying to do my homework my english ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    Read, Write on files.. counting lines.. stuff like that

    Hello everybody.. I'm an italian boy, who has an exam right tomorrow.. I'm trying to do my homework my english is bad, really bad .. sorry for that.

    look, I'm on windows, using cgwin (should be gcc compiler)

    this is what my program should do:

    it should read a file "gironi.dat" where there is a list of soccer teams.. and some statistics like won matches, loses, draw, goals, goals taken

    like this:

    ARSENAL 5 0 1 9 2
    MANCHESTER 4 1 1 8 3
    [..]

    then it has to write another file "classifica.dat" and put there the same list of soccer teams with the total of played matches, point and (goals - goals taken)

    EDIT: I almost forgot, a team's name cant be longer than 10 characters, and the list cant be longer than 18 teams!

    I know my english teacher would be very mad on me after this.. well, this is the code i wrote:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int contasquadre(FILE *read){
    char linee;
    int n;
    
    do {
    linee = getc(read);
    if(linee=="\n") n++;
    }
    while (linee != EOF);
    
    return n;
    }
    
    void caricastruct(struct classifica *pclass, FILE *read){
    fscanf(read, "%s %d %d %d %d %d", (*pclass).nome,(*pclass).vinte,(*pclass)…
    }
    
    void scrivistruct(struct classifica *pclass, FILE *write){
    int partite,punti,differenzareti;
    partite=(*pclass).vinte + (*pclass).perse + (*pclass).pareggiate;
    punti=3*((*pclass).vinte)+(*pclass).p…
    differenzareti=(*pclass).fatti - (*pclass).subiti;
    
    fprintf(write,"%s %d %d %d", (*pclass).nome, partite, punti, differenzareti);
    }
    
    int main(void){
    struct classifica {
    char nome[10];
    int vinte;
    int perse;
    int pareggiate;
    int fatti;
    int subiti;
    };
    struct classifica class;
    struct classifica *pclass;
    FILE *read;
    FILE *write;
    int i,nsquadre;
    
    read=fopen("es22.dat", "r");
    write=fopen("classifica.dat","wb");
    nsquadre=contasquadre(read);
    pclass=&class;
    
    for(i=0; i<nsquadre; i++){
    caricastruct(pclass, read);
    scrivistruct(pclass, write);
    }
    
    fclose(read);
    fclose(write);
    
    return 0;
    }
    and this is what mingw thinks about my code!

    Code:
    $ gcc -o girone girone.c
    girone.c: In function 'contasquadre':
    girone.c:10: warning: comparison between pointer and integer
    girone.c: At top level:
    girone.c:17: warning: "struct classifica" declared inside parameter list
    girone.c:17: its scope is only this definition or declaration, which is probably not what you want
    girone.c:18: error: deferencing pointer to incomplete type
    girone.c:21: warning: "struct classifica" declared inside parameter list
    girone.c: In function 'scrivistruct':
    girone.c:23: error: deferencing pointer to incomplete type
    girone.c:51: warning: passing arg 1 of 'caricastruct' from incompatible pointer type
    girone.c:52: warning: passing arg 1 of 'scrivistruct' from incompatible pointer type
    I'm starting to change my mind about suicide! XD

    thanks to those who'll try to help me. i think i've done some mistake sending pointer to function.. probably there's something else.. the c manual i'm using its not helping..
    Last edited by Darkobra; 07-14-2010 at 03:56 AM.

  2. #2
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    girone.c:10: warning: comparison between pointer and integer
    "\n": using double quotes creates a const char pointer to that string in memory. You want to use single quotes to specify a single char: '\n'.

    Most of the other errors:
    You need to declare your struct first (using typedef, if desired) and then create an instance later when you want to use one. The compiler is complaining because void caricastruct() uses your struct before you define it in main().

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Thanks Epy, really quick!
    Well, it gives me a pair of error yet.. past new errors:

    Code:
    girone.c:50: error: conflicting types for 'caricastruct'
    girone.c:27: error: previous implicit declaration of 'caricastruct' was here
    girone.c:54: error: conficting types for 'scrivistruct'
    girone.c:28: error: previous implicit declaration of 'scrivistruct' was here
    i just don't know why!

    fixed that '\n' and used the typedef, past new code here:

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    typedef struct {
    
    	char nome[10];
    
    	int vinte;
    
    	int perse;
    
    	int pareggiate;
    
    	int fatti;
    
    	int subiti;
    
    	} classifica;
    
    int main(void){
    
    	
    
    	classifica class;
    
    	classifica *pclass;
    
    	FILE *read;
    
    	FILE *write;
    
    	int i,nsquadre;	
    
    	
    
    	read=fopen("es22.dat", "r");
    
    	write=fopen("classifica.dat","wb");
    
    	nsquadre=contasquadre(read);
    
    	pclass=&class;
    
    		
    
    	for(i=0; i<nsquadre; i++){
    
    		caricastruct(pclass, read);
    
    		scrivistruct(pclass, write);
    
    	}
    
    
    
    	fclose(read);
    
    	fclose(write);
    
    	
    
    return 0;
    
    }
    
    
    
    int contasquadre(FILE *read){
    
    	char linee;
    
    	int n;
    
    	
    
    	do { 	
    
    		linee = getc(read);
    
    		if(linee=='\n') n++;
    
    	}
    
    	while (linee != EOF);
    
    
    
    return n;
    
    }
    
    
    
    void caricastruct(classifica *pclass, FILE *read){
    
    	fscanf(read, "%s %d %d %d %d %d", (*pclass).nome,(*pclass).vinte,(*pclass).perse,(*pclass).pareggiate,(*pclass).fatti,(*pclass).subiti);
    
    }
    
    
    
    void scrivistruct(classifica *pclass, FILE *write){
    
    	int partite,punti,differenzareti;
    
    	partite=(*pclass).vinte + (*pclass).perse + (*pclass).pareggiate;
    
    	punti=3*((*pclass).vinte)+(*pclass).pareggiate;
    
    	differenzareti=(*pclass).fatti - (*pclass).subiti;
    
    	
    
    	fprintf(write,"%s %d %d %d", (*pclass).nome, partite, punti, differenzareti);
    
    }
    if anyone can help...

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Line 50? where???
    Put function prototypes before calling functions.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    You mean like this:

    Code:
    ...
    int contasquadre(FILE*);
    int caricastruct(classifica, FILE*);
    int scrivistruct(classifica, FILE*);
    ...
    ?

    It doesn't change a bit.. .___.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    Your prototypes for caricastruct and scrivistruct do not match the definitions. What is the type of the first parameter in the definition?

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Yeah, I know that..

    the first type is classifica!

    Code:
    [..]
    typedef struct {
    	char nome[10];
    	int vinte;
    	int perse;
    	int pareggiate;
    	int fatti;
    	int subiti;
    	} classifica;
    [...]
    ...this is classifica.
    then, in main function i've declareted

    classifica class;
    classifica *pclass;

    and that's what i try to fit in caricastruct and scrivistruct..
    i know i'm newbie and this is an newbie's error.. i just can't figure out what could be..

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    The type of the first parameter of caricastruct and scrivistruct in their definitions is classifica *. Your declarations have classifica not classifica *.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    you're right,
    but the error still remain .-. just tried. .__.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    So you have put the following lines in front of the main function:
    Code:
    int contasquadre(FILE*);
    int caricastruct(classifica *, FILE*);
    int scrivistruct(classifica *, FILE*);
    and you are still getting the identical errors as shown in the last message where you showed errors?

    If you are still having problems, please repost your current code and the current errors.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Here you go!
    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
    
    typedef struct {
    
    	char nome[10];
    
    	int vinte;
    
    	int perse;
    
    	int pareggiate;
    
    	int fatti;
    
    	int subiti;
    
    	} classifica;
    
    
    
    int contasquadre(FILE*);
    
    int caricastruct(classifica *, FILE*);
    
    int scrivistruct(classifica *, FILE*);
    
    
    
    int main(void){
    
    	
    
    	classifica class;
    
    	classifica *pclass;
    
    	FILE *read;
    
    	FILE *write;
    
    	int i,nsquadre;	
    
    	
    
    	read=fopen("es22.dat", "r");
    
    	write=fopen("classifica.dat","wb");
    
    	nsquadre=contasquadre(read);
    
    	pclass=&class;
    
    		
    
    	for(i=0; i<nsquadre; i++){
    
    		caricastruct(pclass, read);
    
    		scrivistruct(pclass, write);
    
    	}
    
    
    
    	fclose(read);
    
    	fclose(write);
    
    	
    
    return 0;
    
    }
    
    
    
    int contasquadre(FILE *read){
    
    	char linee;
    
    	int n;
    
    	
    
    	do { 	
    
    		linee = getc(read);
    
    		if(linee=='\n') n++;
    
    	}
    
    	while (linee != EOF);
    
    
    
    return n;
    
    }
    
    
    
    void caricastruct(classifica * pclass, FILE * read){
    
    	fscanf(read, "%s %d %d %d %d %d", (*pclass).nome,(*pclass).vinte,(*pclass).perse,(*pclass).pareggiate,(*pclass).fatti,(*pclass).subiti);
    
    }
    
    
    
    void scrivistruct(classifica *pclass, FILE * write){
    
    	int partite,punti,differenzareti;
    
    	partite=(*pclass).vinte + (*pclass).perse + (*pclass).pareggiate;
    
    	punti=3*((*pclass).vinte)+(*pclass).pareggiate;
    
    	differenzareti=(*pclass).fatti - (*pclass).subiti;
    
    	
    
    	fprintf(write,"%s %d %d %d", (*pclass).nome, partite, punti, differenzareti);
    
    }
    And now the errors:

    Code:
    girone.c:54: error: conflicting types for 'caricastruct'
    girone.c:14: error: previous declaration of 'caricastruct' was here
    girone.c:54: error: conflicting types for 'caricastruct'
    girone.c:14: error: previous declaration of 'caricastruct' was here
    girone.c:58: error: conflicting types for 'scrivistruct'
    girone.c:15: error: previous declaration of 'scrivistruct' was here
    girone.c:58: error: conflicting types for 'scrivistruct'
    girone.c:15: error: previous declaration of 'scrivistruct' was here

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Check the return types of your function declarations and corresponding function definitions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Yeah, you're right. stupid me!
    edited, i can compile now, but surely i've mistaken something else! it creates classifica.dat as he should, but it writes there some nonsenses..

    aƒ╬ah═" 1631930957 6844463 -1629649438aƒ╬ah═" 1631930957 6844463 -1629649438aƒ╬ah═" 1631930957 6844463 -1629649438

    is it windows fault?

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    No, it's your fault.

    1. Your attempt to count the number of lines in the file results in you leaving the file with an EOF state.

    2. ALL your fscanf() calls will now fail, but you don't check the return result of fscanf.

    3. Your struct variable remains uninitialised, and you output garbage.

    Somewhere, you should do
    fseek( fp, 0, SEEK_SET );
    clearerr( fp );
    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.

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Some funny four-eyed prof told me "you can be sure that when you allocate a struct, it is initialised all 0 by itself!"

    gotcha, check the error, it were not so difficult to understand, i will try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open read write close
    By mercuryfrost in forum C Programming
    Replies: 7
    Last Post: 08-23-2009, 05:27 PM
  2. read / write binary files
    By Dark_Phoenix in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2009, 07:56 AM
  3. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 09:25 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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