Is there any reason why the code below won't work (I mean as far as writing output to file, not as far as writing the correct output to the file):

#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream fout ("ride.out");
    ifstream fin ("ride.in");

    char comet[6] = {0,0,0,0,0,0};
    char group[6] = {0,0,0,0,0,0};
    int comet_buffer = 1;
    int group_buffer = 1;

    fin >> comet;
    fin >> group;
    cout << comet << endl << group;

    for(int i = 0;i < 6;i++)
    {
        if(comet[i] != 0)
        {
            comet_buffer *= (int)(comet[i]-64);
        }
    }
    for(int n = 0;n < 6;n++)
    {
        if(group[n] != 0)
        {
            group_buffer *= (int)(group[n]-64);
        }
    }
    if(comet_buffer%47 == group_buffer%47)
    {
        fout << "GO\n";
    }
    else
    {
        fout << "STAY\n";
    }
    return 0;
}

I have a file called ride.in, and it contains the word COMETQ on one line, and HVNGAT on the second line. The program did not create a file called ride.out. I tried manually creating one and running it again, and the file was still empty after the program ran.

Also, this is for an online programming competition thing (this is practice, not the actual thing). They give an example of code on how to use fstream. I copied the code exactly into my compiler (they encourage you to for testing purposes, so I know the code is a complete program), I run it, and no file is created for output. i.e the program doesn't work. is there anything that might be causing this?

Also worth noting that you submit ur .cpp file to their site, and they compile for you. the test code works when you upload it to their site, just not on my machine. im asking this because the code at the very top works for neither my machine or theirs (though, on mine, it just doesn't write output, on theirs, I get a run time error).

Thanks

Recommended Answers

All 4 Replies

you could check the success/failure of the open

ofstream fout ("ride.out");
if( !fout.is_open())
{
   cout << "Failed\n";
   return 1;
}

[edit]commet and group buffers are too small. You didn't leave room the the null teriminator byte.

Thanks - that helped, but I'm still not really sure what's up.

ride.out opens okay (according to the test, still no output though), but ride.in is failing to open. I don't know why - ride.in is in the same directory as the project file and the .cpp file.

This may be a separate problem, but this is the error that the competition site gives:
Run 1: Execution error: Your program (`ride') exited with signal
#11 (segmentation violation [maybe caused by accessing memory out
of bounds, array indexing out of bounds, using a bad pointer
(failed open(), failed malloc), or going over the maximum
specified memory limit]). The program ran for 0.022 CPU seconds
before the signal. It used 2844 KB of memory.

Even if this is a separate problem, if someone knows why its coming up, it would be nice - because while I'd like to know why this isn't working on my machine - I really only care if it works when I upload it.

[edit]commet and group buffers are too small. You didn't leave room the the null teriminator byte.

That was it - thanks. Still doesn't work on my own machine, but it uploaded fine on the site. Multas gratias!

Thanks - that helped, but I'm still not really sure what's up.
ride.out opens okay (according to the test, still no output though), but ride.in is failing to open. I don't know why - ride.in is in the same directory as the project file and the .cpp file.

This may be a separate problem, but this is the error that the competition site gives:

Run 1: Execution error: Your program (`ride') exited with signal
        #11 (segmentation violation [maybe caused by accessing memory out
        of bounds, array indexing out of bounds, using a bad pointer
        (failed open(), failed malloc), or going over the maximum
        specified memory limit]). The program ran for 0.022 CPU seconds
        before the signal. It used 2844 KB of memory. 

end quote.

Let me make a guess.

ride.in contents
COMETQ
HVNGAT

char comet[7/*6*/] = {0,0,0,0,0,0}; 
char group[7/*6*/] = {0,0,0,0,0,0};

fin >> comet; //will be initialized to COMETQ+null terminating total 7
/*
at this point is the file pointer pointing to the newline+HVNGAT total 7 and declare char group[7] means is not enuff as newline+HVNGAT+null terminating require total 8?
*/
fin >> group;

Does the file pointer discard the newline character after COMETQ ? One way to test is to allocate bigger array size for comet and group variables and then if read successfully from file, iterate through each element in the array to see how it is populated and how many is being read for troubleshooting purposes.

>>Does the file pointer discard the newline character after COMETQ ?
Yes. So only 7 are required in his program. This is unlike C's fgets() where the new-line, when present in the input stream, is appended to the end of the string if there is enough room in the input buffer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.