-2

I've a program wich calculates much numbers, I can't store the data in an array because the RAM-memory isn't big enough for the data. So I wrote a bit of code wich puts the data in an .txt-file. In the same program I have to load it back piece by piece to display the data using openGL/ GLUT.

Now, the .txt file looks like this:

number1;number 2;number3;number4;......number N;
number1;number 2;number3;number4;......number N;
number1;number 2;number3;number4;......number N;
...................................................................................
number1;number 2;number3;number4;......number N;

Note that after number N; comes a "\n" (enter). And that a line is 2500 numbers long.
I have to load the data line by line so I use getline(); But every frame needs the next line.

To simplify a bit, here is the code to read:

ifstream file("example.txt", ifstream::in);
if(file.is_open())
{
    getline(file, b);
    cout<<b<<"\n"<<"\n";
    file.close();
}

This code is not the code in the real program but it displays the problem.

The code works fine here, it simple loads the first line and displays it on the console screen. And does it each frame in openGL. so my console screen is continously updated every time opengl starts a new frame.

but when I add this:

ifstream file("example.txt", ifstream::in);
if(file.is_open())
{
    getline(file, b);
    strcpy(resultch, b.c_str());
    cout<<b<<"\n"<<"\n";
    file.close();
}

(Were resultch is declared as: char* resultch = new char[2550];) The code only runs 1 time and the program stops after it, the console screen than says:

proces returned -1073741819 <0x0000005>

Why does it not properly run each time?

yzt
  • 8,873
  • 1
  • 35
  • 44
abcdef
  • 236
  • 1
  • 5
  • 17
  • 1
    Is b.c_str() defined when this starts? I think 0x00000005 is an access violation. – ethrbunny Mar 28 '13 at 17:13
  • sorry, but i don't know really what .c_str() means but how do you have to difine it? Isn't it a kind of a function? – abcdef Mar 28 '13 at 17:15
  • 2
    Its probably something like a cast to const char* (or similar). Why are you copying the value into resultch before you read it anyway? – ethrbunny Mar 28 '13 at 17:17
  • Mistake in the writing of the question, but in the actuall code it is after the getline – abcdef Mar 28 '13 at 17:18
  • as a string, can I declare b as a double? because I assume that a .txt file is a string, but I'm not sure about this – abcdef Mar 28 '13 at 17:25
  • 2
    Did you close your file in your loop? Your actually code is needed. The code above seems good as far as my test's concerned. – gongzhitaao Mar 28 '13 at 17:26
  • For now, this is the actual code, but I made a program in c++ with a getline wich isn't big, maybe strcpy won't work with big strings? closing the file is in the loop – abcdef Mar 28 '13 at 17:31
  • What's the value of b.length()? – ethrbunny Mar 28 '13 at 17:33
  • 1
    Oh 36701, while it's supposed to be 2500 – abcdef Mar 28 '13 at 17:40
  • 1
    @abcdef: You say that "And that a line is 2500 numbers long." What does this mean? Does it mean that each line in the file is 2500 *characters* long? Or that there are 2500 *numbers* on each line? Because if there are 2500 numbers, then the number of characters on each line will be much higher and the size of the character array is not enough to hold them. – yzt Mar 28 '13 at 17:41
  • @abcdef: You can do something like this: after the `getline()`, you do `char* resultch = new char [b.size() + 1];` and then you `strcpy` into it. By the way, don't forget to `delete[] resultch;` when you are done with it. And remember that using `strncpy()` is much safer than `strcpy()`. – yzt Mar 28 '13 at 17:44
  • Idd, 2500 numbers so more characters – abcdef Mar 28 '13 at 17:52
  • 2
    @abcdef: Why are you copying `b` into `resultch`? I'm sensing that you are a little confused about what a string is and what you can and can't do with a `std::string`. Why do you need `resultch`? – yzt Mar 28 '13 at 17:53
  • 1
    @abcdef: Also, are you actually trying to only read the first line, or do you want to read all the lines? – yzt Mar 28 '13 at 17:55
  • By the way, per author's comment, I fixed the code in the question (swapped the `getline` and the `strcpy` in the second block of code) as it seemed the author weren't going to. – yzt Mar 28 '13 at 18:00
  • So the problem is that you haven't allocated enough memory. – ethrbunny Mar 28 '13 at 19:21

2 Answers2

0

What if you:

ifstream file("example.txt", ifstream::in);
if(file.is_open())
{
    getline(file, b);
    resultch = malloc( b.length() + 1 );
    strcpy(resultch, b.c_str());
    cout<<b<<"\n"<<"\n";
    file.close();

    free( resultch );
}

All I added was to allocate memory based on the size of the string.

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
  • I believe you are not allocating enough memory either. It seems to me that you've missed the NUL character (`'\0'`) that is needed at the end of `resultch`. – yzt Mar 30 '13 at 06:25
  • So noted. Edited to include. – ethrbunny Mar 30 '13 at 12:27
-1

Try this:

ifstream infile;
infile.open ("example.txt", ifstream::in);

std::string line("");

int ch = infile.get();
while (infile.good()) {
    if ((char)ch=='\n'){
         resultch = line.c_str();
         line = "";
         cout << endl << endl;
    }
    else {
         line = line + (char)ch;
         cout << (char)ch; 
    }
    ch = infile.get();
}
infile.close();

That's probably what you looking for.

VitorMM
  • 1,060
  • 8
  • 33
  • This is completely misleading, inefficient, error-prone and outright incorrect. The free function `std::getline (file, str);` does all the above. Incidentally, that's the same function that OP is using in her code, and that's not where the problem lies. Please don't recommend wrong solutions and bad practices. – yzt Mar 28 '13 at 17:49
  • I don't know if it's slow, but it's not wrong. This code do exactly what she wanted to do in the second code, but in a different way. Char by char instead of Line by Line. – VitorMM Mar 29 '13 at 05:28
  • And what would be the advantage of reading it character by character? (byte by byte, actually.) All I'm saying is that your code, does exactly what `std::getline` does too. So in this case, what would be the point of it? What problem are you solving here? – yzt Mar 30 '13 at 06:22
  • And your code is problematic at best, if not outright wrong. You assign `line.c_str()` directly to `resultch`, without allocating memory, which just copies a pointer (if even legal without a cast.) This means that the lifetime of the string pointed to by `resultch` is implicitly dependent on `line` remaining alive and *unchanged*. This is, again at best, unsafe and bad coding practice. – yzt Mar 30 '13 at 06:30