Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Having Troubles to read file in c++

  1. #1
    Join Date
    Oct 2008
    Beans
    68

    Having Troubles to read file in c++

    Hi everyone.
    I am still playing with files and had some trouble
    I have the code in where I write the files to the file
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main (){
     FILE *archivo=0;
     archivo=fopen("prueba.dat","a");
     char *name=new char [30];
     int codigo,sueldo,contador,op;
     do{
         cout<<"Ingrese 0 si desea salir y cualquier numero si desea continua";
         cin>>op;
         cout<<"Ingrese codigo: ";
         cin>>codigo;
         cout<<"Ingrese nombre: ";
         cin>>name;
         cout<<"Ingrese sueldo: ";
         cin>>sueldo;
         fwrite(&codigo,1,sizeof(codigo),archivo);
         //fseek(archivo,4,SEEK_SET);
         fwrite(&name,1,sizeof(name),archivo);
         //fseek(archivo,sizeof(name),SEEK_SET);
         fwrite(&sueldo,1,sizeof(sueldo),archivo);
         //fseek(archivo,4,SEEK_SET);
    
     }while(op!=0);
     fclose(archivo);
    
    }
    Here it is the code, where I am reading the file. But it doesn't do the code it has to do
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main (){
        FILE *archivo=0;
        char *name=new char [30];
        archivo=fopen("prueba.dat","rb");
        int sueldo,codigo;
    
        while(!feof(archivo)){
            fread(&codigo,1,sizeof(codigo),archivo);
            cout<<codigo<<" ";
            fread(&name,1,sizeof(name),archivo);
            cout<<name<<" ";
            fread(&sueldo,1,sizeof(sueldo),archivo);
            cout<<sueldo<<" ";
    
        }
    
        fclose(archivo);
    
    
    
    }

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Having Troubles to read file in c++

    So first of all, have you checked that the file has been written correctly?

    Then, what are the errors that you get? You didn't say what is actually wrong.

    Finally, you didn't check whether the calls to "fopen" or "fread" actually succeed. Go and check their return values!

  3. #3
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Having Troubles to read file in c++

    again a typical sizeof misuse

    fwrite(&name,1,sizeof(name),archivo);

    will give you the size of the pointer 4 and not what you probably want 30
    sizeof only works in sizes known to the compiler, that is structures data types and stack!! arrays which have not decayed into pointers
    and name is already a pointer, so the & operator is wrong, this is better:
    fwrite(name,1,30,archivo);
    same bug when reading


    also you should open your file in write binary mode wb instead of only w (although I don't really think that matters much)
    and c++ has own file functions in the iostream library
    its recommended to use these
    Last edited by MadCow108; February 3rd, 2010 at 03:39 PM.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Having Troubles to read file in c++

    @ DiegoTc

    Your writing code in C++; thus why not use fstream (and string) for your file writing/reading needs? It would make your life a lot easier.

    For example:
    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
       using namespace std;
    
       fstream file("prueba.dat", ios::out | ios::app);
    
       if (file)
       {
          while (true)
          {
             unsigned int numero = 0;
             unsigned int codigo = 0;
             string       name;
             unsigned int sueldo = 0;
    
             cout << "Entre 0 para terminar, o otro numero para continuar: ";
             cin  >> numero;
    
             if (numero == 0) break;
    
             cout << "Ingrese codigo: ";
             cin  >> codigo;
    
             cout << "Ingrese nombre: ";
             cin  >> name;
    
             cout << "Ingrese sueldo: ";
             cin  >> sueldo;
    
             file << codigo << name << sueldo << endl;
          }
       }
       else
       {
          cerr << "No se puede abrir prueba.dat" << endl;
          return -1;
       }
    
       return 0;
    }
    If you are looking to store the information in binary, then use the ios::binary option when opening the file. Also, use the write() method.

    You can read more about fstream here:
    http://www.cplusplus.com/reference/iostream/fstream/
    Last edited by dwhitney67; February 3rd, 2010 at 03:42 PM.

  5. #5
    Join Date
    Oct 2008
    Beans
    68

    Re: Having Troubles to read file in c++

    Atually the first program works correctly
    I can insert all the data I ask the user.
    When I run the second program. It only shows me the first 1 number of the file. Then it stops working

  6. #6
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Having Troubles to read file in c++

    have you fixed the problems I mentioned?
    in the unfixed one it obviously will fail after the first fread because the second fwrite/fread combo is wrong, the first one is fine.

  7. #7
    Join Date
    Oct 2008
    Beans
    68

    Re: Having Troubles to read file in c++

    @dwhitney67
    Yes I know it that way. But i want to try the cstdio lib.
    And what I am seeing is that the file works fine, when it is not reading a string.
    SO i think I am making bad the process of reading it, but i can't identify it

    @MadCow108
    Yes I fix it but always the same error

    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main (){
     FILE *archivo=0;
     archivo=fopen("prueba.dat","wb");
     char *name=new char [6];
     int codigo,sueldo,contador,op;
     do{
         cout<<"Ingrese 0 si desea salir y cualquier numero si desea continua";
         cin>>op;
         cout<<"Ingrese codigo: ";
         cin>>codigo;
         cout<<"Ingrese nombre: ";
         cin>>name;
         cout<<"Ingrese sueldo: ";
         cin>>sueldo;
         fwrite(&codigo,1,sizeof(codigo),archivo);
         fwrite(&sueldo,1,sizeof(sueldo),archivo);
         fwrite(&name,1,6,archivo);
    
     }while(op!=0);
     fclose(archivo);
    
    }
    And the second program it will be like this
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main (){
        FILE *archivo=0;
        char *name=new char [6];
        archivo=fopen("prueba.dat","rb");
        int sueldo,codigo;
    
        while(!feof(archivo)){
            fread(&codigo,1,sizeof(codigo),archivo);
            cout<<codigo<<" ";
            fread(&sueldo,1,sizeof(sueldo),archivo);
            cout<<sueldo<<" ";
            fread(&name,1,6,archivo);
            cout<<name<<" ";
    
        }
    
        fclose(archivo);
    
    
    
    }
    So i am still wondering which one is the problem :S
    Last edited by DiegoTc; February 3rd, 2010 at 04:29 PM.

  8. #8
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Having Troubles to read file in c++

    edit:
    you still have the & operator to much for the string

    its not needed for the pointer, as it already is a pointer

    Code:
    char *name=new char [6];
    fwrite(&name,1,6,archivo);
    here you try to write 6 bytes from the address of the name pointer to the file, the location the address of name points to only holds 4 bytes (or 8bytes on 64 bit systems) because name is a pointer already
    you want to write the 6 bytes name points to:
    fwrite(name,1,6,archivo);
    Last edited by MadCow108; February 3rd, 2010 at 04:34 PM.

  9. #9
    Join Date
    Oct 2008
    Beans
    68

    Re: Having Troubles to read file in c++

    @MadCow108 Sorry i click in reply with out copying.
    The problem still continue.
    It only prints the "codigo" and the "salario", when it has to print the name the program stops running

  10. #10
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Having Troubles to read file in c++

    your exact same code with the corrected & works on my system
    (except it prints twice because feof only gets set on the second iteration with the reads failing then)

    maybe use valgrind on it to pinpoint your problem
    (the uninitialized syscall error you'll get is superfluous in this simple case, make it disappear by initializing your string buffer)
    Last edited by MadCow108; February 3rd, 2010 at 04:47 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •