problems with opening files and pointers...

This is a discussion on problems with opening files and pointers... within the C Programming forums, part of the General Programming Boards category; hey! i'm starting to learn c programming language and i'm having a lot of trouble with analising a file and ...

  1. #1
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24

    problems with opening files and pointers...

    hey!
    i'm starting to learn c programming language and i'm having a lot of trouble with analising a file and read its characters and then copy them to another file... also i have a lot of problems with pointers and arrays... here's my code and remember n00b alert!!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char incluir[]={'#','i','n','c','l','u','i','r',' ','\"'};
    char definir[]={'#','d','e','f','i','n','i','r',' '};
    char esquecer[]={'#','e','s','q','u','e','c','e','r',' '};
    
    void fazincluir(linha){
      int i=9;
      int j=0;
      int caracteracopiar;
      char nomedofile[1024];
      while (&linha[i] != '\"'){
        nomedofile[j]=linha [i];}
      nomedofile[j]='\0';
      fopen(nomedofile, "r");
      while((caracteracopiar=fgetc(atoi(nomedofile))) != EOF)
        fputc(caracteracopiar, ficheiroderesultado);
    }
    
    
    void analisa(FILE* ficheiro){
      char* linha=(char*) malloc(sizeof(char)*1024);
      while((fgets(linha, pre)) != EOF){
        if ((strncmp(incluir, linha, 9)) > 0)
          fazincluir(linha);
        else if ((strncmp(esquecer, linha, 10)) > 0)
          fazesquecer(linha);
        else if ((strncmp(definir, linha, 9)) > 0)
          fazdefinir(linha);
      }
    }
    
    int main(int argc, char* argv[]){
      FILE* pre;
      pre=fopen (argv[1], "r");
      if(pre == NULL)
        printf("Erro: Impossivel abrir ficheiro\n");
      else analisa(pre);
      return 0;
    }
    i get the following compilation errors:
    Code:
    proj2-1.c: In function `fazincluir':
    proj2-1.c:14: subscripted value is neither array nor pointer
    proj2-1.c:15: subscripted value is neither array nor pointer
    proj2-1.c:18: warning: passing arg 1 of `fgetc' makes pointer from integer without a cast
    proj2-1.c:19: `ficheiroderesultado' undeclared (first use in this function)
    proj2-1.c:19: (Each undeclared identifier is reported only once
    proj2-1.c:19: for each function it appears in.)
    proj2-1.c: In function `analisa':
    proj2-1.c:25: `pre' undeclared (first use in this function)
    proj2-1.c:25: too few arguments to function `fgets'
    proj2-1.c:25: warning: comparison between pointer and integer
    proj2-1.c:29: warning: implicit declaration of function `fazesquecer'
    proj2-1.c:31: warning: implicit declaration of function `fazdefinir'
    PS: nevermind the implicit declarations, the program is not done yet.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You really should get a good book on C, because from your code it's obvious you have no idea how to use most standard functions, the differences in type and scope, and various other features of C:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char incluir[]={'#','i','n','c','l','u','i','r',' ','\"'};
    char definir[]={'#','d','e','f','i','n','i','r',' '};
    char esquecer[]={'#','e','s','q','u','e','c','e','r',' '};
    
    extern FILE *ficheiroderesultado;
    FILE* pre;
    
    void fazincluir(char *linha){
      int i=9;
      int j=0;
      int caracteracopiar;
      char nomedofile[1024];
      FILE *file;
      while (linha[i] != '\"'){
        nomedofile[j]=linha [i];}
      nomedofile[j]='\0';
      file=fopen(nomedofile, "r");
      while((caracteracopiar=fgetc(file)) != EOF)
        fputc(caracteracopiar, ficheiroderesultado);
    }
    
    
    void analisa(FILE* ficheiro){
      char* linha=(char*) malloc(sizeof(char)*1024);
      while((fgets(linha, 1024, pre)) != NULL){
        if ((strncmp(incluir, linha, 9)) > 0)
          fazincluir(linha);
        else if ((strncmp(esquecer, linha, 10)) > 0)
          fazesquecer(linha);
        else if ((strncmp(definir, linha, 9)) > 0)
          fazdefinir(linha);
      }
    }
    
    int main(int argc, char* argv[]){
      pre=fopen (argv[1], "r");
      if(pre == NULL)
        printf("Erro: Impossivel abrir ficheiro\n");
      else analisa(pre);
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    > char incluir[]={'#','i','n','c','l','u','i','r',' ','\"'};
    The easy way
    char incluir[]= "#incluir \"";
    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
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    well i have two books, c programming language by rithchie and one in portuguese... guess i gotta read them better before i come here asking these type of questions...
    but thanks for the help you guys! you rule!!!!

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    }
    }

    int main(int argc, char* argv[]){
    FILE* pre;
    pre=fopen (argv[1], "r");
    if(pre == NULL)
    printf("Erro: Impossivel abrir ficheiro\n");
    else analisa(pre);
    return 0;
    }
    dans cette partie du code il a une erreur fatale
    c'est de fermer '}' sans aucune precedent
    et encore fermer une autre sans ouvrir '{' au debut du programme main
    ===> fait attention il faut utilser la bibliotheque <fstrem.h> pour la gestion des fichier
    ....good luck

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by enjoy
    }
    }

    int main(int argc, char* argv[]){
    FILE* pre;
    pre=fopen (argv[1], "r");
    if(pre == NULL)
    printf("Erro: Impossivel abrir ficheiro\n");
    else analisa(pre);
    return 0;
    }
    dans cette partie du code il a une erreur fatale
    c'est de fermer '}' sans aucune precedent
    et encore fermer une autre sans ouvrir '{' au debut du programme main
    ===> fait attention il faut utilser la bibliotheque <fstrem.h> pour la gestion des fichier
    ....good luck
    There isn't anything wrong with the bracing, and fstream is C++. This is the C forum.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create an array of pointers to files
    By alavardi in forum C Programming
    Replies: 6
    Last Post: 03-05-2005, 08:35 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. passing pointers to other files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 02-01-2005, 11:05 PM
  4. pointers into largish text files
    By donkeypunch in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2004, 03:04 PM
  5. Help on opening files
    By drharv in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2002, 11:16 PM

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