I wonder why this program does not do any changes to the text file that I am trying to modify.
Scenario: My program uses the Fstream header file, I declared infile as IFSTREAM and outfile as OFSTREAM. I simply want to transfer data from infile to outfile. The data is as follows;

Adam Larson 89 90

This data is saved in a text file manually created with Notepad. Furthermore, I also want to add the average of 89 and 90 into the outfile target file however, it doesn't do it. it does nothing at all. I also observed that there seems to be no data extracted at all using the infile variabe so nothing is being copied into the output file. here is my code;

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){

    ifstream infile;
    ofstream outfile;
    infile.open("C:\Projects\C++\infile.txt");
    outfile.open("C:\Projects\C++\outfile.txt");

    string fname, lname;
    int mathgrade, sciGrade;
    double average;

    //get data from Input file
    cout << "This program creates a copy of a student data file with average\n";
    infile >> fname >> lname;
    infile >> mathgrade >> sciGrade;
    average = (mathgrade + sciGrade) / 2;
    //copy data into Output file
    outfile << fname << " " << lname << " " << mathgrade << " " << sciGrade << " " << endl << "Average: " << average << endl;
    outfile.close();
    infile.close();
    cout << "Done processing...\nCheck thie file for output: C:\Projects\C++\outfile.txt\n";
    system("pause");
    return 0;
}

What happens if you rewrite lines 9 and 10 to use forward slash rather than backward slash?

 infile.open("C:/Projects/C++/infile.txt");
outfile.open("C:/Projects/C++/outfile.txt");

backslashes should be \\ since \ is an escape character. Try:

infile.open("C:\\Projects\\C++\\infile.txt");
outfile.open("C:\\Projects\\C++\\outfile.txt");

While what NathamOliver says is true, note that forwardslash works "everywhere" (by which I mean, on windows and *nix and on Solaris and various other places as well).

Now I am wondering why I haven't thought of that???
lol.
That's exactly it. Thanks guys. I shall give you credits here.

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.