Can I make this code work on VS 2010?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    string line, new_content;
    string prefix   = "1.";
    char filename[] = "DATA.txt";

    // Read lines and prepare contents by prefixing lines with a prefix
    ifstream infile(filename);
    while ( infile >> line ) {
        new_content += line + prefix + "\n";
    }
    infile.close();

    // Write contents
    ofstream outfile(filename);
    outfile << new_content;
    outfile.close();

    return 0;
}
It only works on CB. I don't know what to do to get it work, it would be useful to know
Last edited on
https://zellwk.com/blog/asking-questions/

Please be more specific. What does not work mean? Does it not compile? Does it crash? Does it just show an empty console?

Because I'm training to be a psychic, I am going to assume that your file is opening correctly in CB (which I happen to know stands for Code::Blocks, also due to my psychic abilities), but incorrectly in Visual Studio 2010.

• Code::Blocks, if I'm not mistaken, has its working directory as the same directory that your project file is in, by default.
• Visual Studio, on the other hand, has its working directory as the same directory that your .exe file is output into, by default.

Make sure you place your DATA.txt in the appropriate folder.

To see what directory your program is being run from, add the following line of code to the beginning of your program:
system("cd"); // only use for testing purposes
Last edited on
Sorry, I didn't mention, it didn't compile it. But I solved it, thanks for help. I found answer here https://stackoverflow.com/questions/1631338/error-c2679-binary-no-operator-found-which-takes-a-right-hand-operand-of?answertab=active#tab-top
Topic archived. No new replies allowed.