Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Having Troubles to read file in c++

  1. #11
    Join Date
    Oct 2008
    Beans
    68

    Re: Having Troubles to read file in c++

    @MadCow108 thans for telling me that. Right i am using codeblocks in windows on my and it is the second time it happens this. It may be the version. I will try it on my laptop with ubuntu and codeblocks

  2. #12
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Having Troubles to read file in c++

    For the first program, where are you setting the value of 'op'?
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

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

    Re: Having Troubles to read file in c++

    are you maybe using a to long string?
    your buffer is only 6 bytes long
    that means it only takes 5 characters!
    you also need room for the null termination to print the cstring correctly

  4. #14
    Join Date
    Oct 2008
    Beans
    68

    Re: Having Troubles to read file in c++

    nop the string is "abc"
    Si i think it is the codeblocks that is failing. One friend of my tells me he has the same trouble with codeblocks in windows

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

    Re: Having Troubles to read file in c++

    It's highly unlikely that CodeBlocks is failing as it is only an IDE and no compiler. Try running your program with valgrind, as suggested, and see what comes out. Also *do* check the return values of fopen and fread, as the behaviour you mention suits to the case in which the last read operation fails. In that case, "name" stays uninitialised which might result in a program crash.

    Also try using a hex editor to see if the generated file looks as expected.

  6. #16
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Having Troubles to read file in c++

    Quote Originally Posted by DiegoTc View Post
    @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
    Haga lo que quieres.

    The problem you may be having is with writing the binary data to the file.

    Here's two working programs, the first that writes the data, the second that reads it in.

    writer.cpp:
    Code:
    #include <iostream>
    #include <cstdio>
    
    int main()
    {
       using namespace std;
    
       FILE* archivo = fopen("prueba.dat", "a");
    
       if (archivo)
       {
          while (true)
          {
             unsigned int codigo, sueldo, op;
             char         nombre[6] = {0};
    
             cout << "Ingrese 0 si desea salir, o cualquier otro numero si desea continuar: ";
             cin  >> op;
    
             if (op == 0) break;
    
             cout << "Ingrese codigo: ";
             cin  >> codigo;
             cin.ignore(1024, '\n');   // clear the input stream before continuing
    
             cout << "Ingrese nombre: ";
             cin.read(nombre, sizeof(nombre) - 1);
             cin.ignore(1024, '\n');
    
             cout << "Ingrese sueldo: ";
             cin  >> sueldo;
             cin.ignore(1024, '\n');
    
             fwrite(&codigo, 1, sizeof(codigo), archivo);
             fwrite(nombre,  1, sizeof(nombre), archivo);
             fwrite(&sueldo, 1, sizeof(sueldo), archivo);
          }
    
          fclose(archivo);
       }
       else
       {
          cerr << "Error abriendo prueba.dat" << endl;
          return -1;
       }
    
       return 0;
    }
    reader.cpp:
    Code:
    #include <iostream>
    #include <cstdio>
    
    int main()
    {
       using namespace std;
    
       FILE* archivo = fopen("prueba.dat", "r");
    
       if (archivo)
       {
          while (true)
          {
             unsigned int codigo, sueldo;
             char         nombre[6] = {0};
    
             fread(&codigo, 1, sizeof(codigo), archivo);
             fread(nombre,  1, sizeof(nombre), archivo);
             fread(&sueldo, 1, sizeof(sueldo), archivo);
    
             if (feof(archivo)) break;
    
             cout << "codigo: " << codigo << "\n"
                  << "nombre: " << nombre << "\n"
                  << "sueldo: " << sueldo << "\n"
                  << endl;
          }
    
          fclose(archivo);
       }
       else
       {
          cerr << "Error abriendo prueba.dat" << endl;
          return -1;
       }
    
       return 0;
    }

Page 2 of 2 FirstFirst 12

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
  •