Thread: Refresh a file

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Refresh a file

    Hello everyone, I might be need your help in a thing that has been bothering me.

    I have a function called map() that creates a file called 'merdas.txt' and writes to it.
    After that, I call a function called map2() that collects the information inside 'merdas.txt' and separates it in a few files.

    Now, this is working, thing is, it only works this way.

    I compile the executable 1st time :

    'merdas.txt' is created.

    I compile the executable 2nd time :

    The few files separately are created.

    This is my function that separates 'merdas.txt' in a few :

    Code:
    void map2(){
        FILE* fpp;
        FILE* fppp;
        int i=0;
        int chave;
        int linhas=0;
        size_t len;
        char* fich;
        char valor[MAXSTRING];
        fpp = fopen("merdas.txt","r");
        if (fpp == NULL) { printf("Este ficheiro ainda não existe \n");}
        else {printf("Existe o ficheiro \n");}
        while(fscanf(fpp,"%d  %s",&chave,&valor)!=-1){
            //printf("chave:%d\nvalor:%s\n",chave,valor);
            const char *str = coiso(chave);
            printf("Vou começar a imprimir os nomes \n");
            printf("str: %s\n",str);
            fppp=fopen(str,"a+");
            //printf("criou o ficheiro\n");
            fprintf(fppp,"%s\n",valor);
            ficheiros[chave]=1;
        }
        fclose(fpp);
    }
    It tells me the file exists so I have no idea why it ain't creating the few files.

    Thanks.

  2. #2
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I managed to find out that when he reads the file, this file is empty.
    Is there any way I can refresh a folder in C?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    One thing I notice it that you need to study the documentation for fscanf(), you're not using it correctly. Another thing are you sure the open inside your loop is working correctly? You need to check the return value of the fopen() function.

    How is coiso() defined and implemented?

    Also if your file doesn't open you really should take some action, like maybe exiting the program.

    Jim

  4. #4
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by jimblumberg View Post
    One thing I notice it that you need to study the documentation for fscanf(), you're not using it correctly. Another thing are you sure the open inside your loop is working correctly? You need to check the return value of the fopen() function.

    How is coiso() defined and implemented?

    Also if your file doesn't open you really should take some action, like maybe exiting the program.

    Jim
    Hello Jim, thanks for your reply.

    I'm well aware but fscanf is working, as I've mentioned in the first post, it's working and creating the textfiles, it's only doing that when I compile the program the second time.

    Coiso() function is also working fine, I'll show how it's implemented though :

    Code:
    const char *coiso(int chave)
    
    
    {
        
        static char str[50];
        
        sprintf(str, "%d.txt", chave);
        
        return str;
        
    }
    My file is opening, I'm only being able to read what's inside when I compile the second time.
    I'm guessing that what I've wrote in the map() function is not being wrote quick enough for the map2() to be able to read it ?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by DeanWinchester View Post
    My file is opening, I'm only being able to read what's inside when I compile the second time.
    I'm guessing that what I've wrote in the map() function is not being wrote quick enough for the map2() to be able to read it ?
    Sounds like output buffering. Try calling
    Code:
     fflush(fp);
    Also make sure you fclose the file when you're done with it, as that'll also flush all the output.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm well aware but fscanf is working,
    Only if you're extremely lucky. You are invoking undefined behavior in the following fscanf().
    Code:
    fscanf(fpp,"%d  %s",&chave,&valor)
    How do you send the address of a string to a function? Hint: Not the way you're doing it.

    Next, you're trying to reuse an open file pointer. Maybe you should close the file and reset any error flags before you try to reuse the file pointer (move the fclose(fppp) inside the loop).

    Jim

  7. #7
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by smokeyangel View Post
    Sounds like output buffering. Try calling
    Code:
     fflush(fp);
    Also make sure you fclose the file when you're done with it, as that'll also flush all the output.
    Thanks buddy, I was missing a fclose().

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constant refresh
    By s_jsstevens in forum Game Programming
    Replies: 7
    Last Post: 08-14-2007, 06:11 PM
  2. Refresh a dos box???
    By ryan_germain in forum C++ Programming
    Replies: 8
    Last Post: 07-02-2004, 05:26 AM
  3. Refresh ProgressBar
    By Dimeslime in forum C# Programming
    Replies: 2
    Last Post: 10-22-2003, 02:47 PM
  4. XP Refresh Rate
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-24-2001, 09:37 AM
  5. BDEdit Refresh - 2
    By JPedro in forum C++ Programming
    Replies: 0
    Last Post: 09-04-2001, 02:45 AM