Thread: Simple C question (i think)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    4

    Simple C question (i think)

    hey there people!
    well, I've got here some code for a program I'm doing and was wondering if you could help me.

    Code:
    function disciplinas=ler_disciplinas(ficheiro)
    	fid2=fopen(ficheiro,"r");
    	xx=list();
    
    	while !feof(fid2)
    		a=ler_disciplina(fid2);
    		xx=append(xx,a);
    	endwhile
    
    	fclose(fid2);
    
    disciplinas=xx;
    
    endfunction
    well, the problem here is that to run this program i can read from 3 different files , "analise.txt", "computacao.txt" and "fisica.txt".

    What I want is to, for ex., if I run :
    ler_disciplinas("analise.txt")

    all the read data is added to a list named analise.

    If I run:
    ler_disciplinas("computacao.txt")

    all the read data is added to a list named computacao.

    Now the program is always adding the data to the list "disciplinas", always overwriting...

    So, can you guys help me?

    thanks in advance

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Look into opening the file with in append mode ("a" or "a+"). As for the rest, I don't think I really understand what this "disciplinas" file is, as I've only seen you mention the files analise, computacao and fisica...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    4
    disciplinas is just the name of the list i want to make.
    if the user runs the program to read from analise.txt, i want the list to be named "analise" instead of "disciplinas"... the same works for computacao e fisica...

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    4
    here is the analises.txt file:

    Code:
    18293	12	04/06/2004	13	24/06/2004
    18789	13	10/01/2005	15	30/01/2005
    19046	7	04/06/2004	0	24/06/2004
    19214	16	10/01/2006	17	30/01/2006
    19436	11	04/06/2005	6	24/06/2005
    19850	12	04/06/2004	15	24/06/2005
    19851	14	10/01/2005	13	24/06/2005
    19896	12	10/01/2006	11	30/01/2006
    20180	13	04/06/2004	14	24/06/2004
    20234	18	04/06/2004	17	24/06/2004
    20270	11	10/01/2005	8	30/01/2005
    20381	7	04/06/2005	0	24/06/2005
    20635	14	10/01/2006	13	30/01/2006
    20977	18	10/01/2006	15	30/01/2006
    what it does is read each line, make a structure for each one, and in the end add all of them to a list.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    so what you want is that the program reads all these files and then puts all their data into another file called disciplinas ? Or is disciplinas just an array of some sort ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    4
    no, the program reads the file chosen "analises.txt", "computacao.txt" or "fisica.txt" and then puts the read data in a list with the name of the file.
    so, if i read "analise.txt" the list where the data is , is called "analise". and if I read "computacao.txt" i want the list to be called "computacao".

    how can i do that?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like

    Code:
    list analise, computacao;
    if ( strcmp( filename, "analises.txt" ) == 0 ) {
      FILE *fp = fopen(filename,"r");
      while ( fgets( buff, sizeof buff, fp ) != NULL ) {
        append( &analise, buff );
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM