-1

After I have written into a file when reading it I get unexpected output.
The code I wrote is :

#include<fstream.h>
#include<conio.h>
#include<string.h>

struct test
{
    char que[100];
    char ans[20];
};

int main()
{
    test s, d;
    clrscr();
    ofstream out("test.dat", ios::binary | ios::app);
    ifstream in("test.dat", ios::binary | ios::in);
    strcpy(s.que, "2.How many ways the letters of the word abas be arranged to  form words with or without meaning");
    strcpy(s.ans, "180");
    out.write((char*) &s, sizeof(test));

    while(!in.eof())
    {
        in.getline((char*) &d, sizeof(test));
        cout << d.que << '\n' << d.ans;
    }

    getch();
    return 0;
}

The output that I get is :

2.How many ways the letters of the word abas be arranged to form words with or w
ithout meaning
180
180

This is the output I get along with some arbitrary characters in between.

What have I done wrong? Why is the string that I stored in s.ans written into s.que as well?

LogicStuff
  • 18,687
  • 6
  • 49
  • 70
TESLA____
  • 1,019
  • 7
  • 8
  • 1
    [while(!in.eof())](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) may be not related to your errors. But when please will you take the message how to use your frickin' debugger before asking such questions here? – πάντα ῥεῖ Aug 28 '15 at 19:49
  • More info: http://stackoverflow.com/help/mcve , http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/quality-standards-error – πάντα ῥεῖ Aug 28 '15 at 19:57
  • 1
    _`char que[100];`_ Also use something better like `string que;` IIRC even ancient turbo c++ supported that. – πάντα ῥεῖ Aug 28 '15 at 20:10
  • Try using `strlen` on `que` to see how many characters are in your *string*. BTW, spelling out variable names makes your code easier to read and easier to debug; the variable name doesn't affect execution time nor build time. – Thomas Matthews Aug 28 '15 at 20:31
  • I tried to debug your program on my computer, but you are using `clrscr` which I don't have and also, I don't have your input file. Looks like you'll have to debug your program for me. – Thomas Matthews Aug 28 '15 at 20:34
  • 1
    Why are you using `getline` to read in a binary file? Remember, `getline` will stop at the first *newline*. So if your `que` variable in the structure has `\n` in it, the `getline` function won't read the second array. Reminder: if you use `write` for output, use `read` for input. – Thomas Matthews Aug 28 '15 at 20:37

1 Answers1

0

I got this to work with the following changes:

  1. Add out.close() after writing to the file to flush the output buffer.
  2. Replace getline with read, to retrieve the bytes as write wrote them. getline might give different results under some conditions.
  3. Move the read statement into the while condition:

    while(in.read((char *) &d, sizeof(test)))

With the read statement in the body of the loop in.eof() will not return true immediately after reading the last test object so the loop will then execute one last time.

Zexuo
  • 11
  • 2