in C I have to write a program that does the following Write
(in C++)
I have to write a program that does the following:
Write a program that reads a file containing only integers, but some of the integers have embedded commas (as in 145, 020). The program should copy the information to a new file, removing any commas from the information. Do not change the number of values per line in the file.
so far i have this:
#include
 #include
using namespace std;
 int main()
 {
    const char NEWLINE = \'\ \';
    const char TAB = \'\\t\';
    const char COMMA = \',\';
    char input_char = 1;
    ifstream infile(\"d:\\\\withcommas.txt\");
    if (infile.fail())
    {
        cerr << \"error opening withcommas.txt\ \";
        exit(-1);
    }
    ofstream output;
    output.open(\"d:\\\\withoutcommas.txt\");
    if (output.fail())
    {
        cerr << \"error opening withoutcommas.txt\ \";
        exit(-1);
    }
    while (!infile.eof())
    {
        if (input_char == COMMA)
            output << \"\\t\";
        else
        {
            int x;
            infile >> x;
            output << x << endl;
        }
    }
    infile.close();
    output.close();
    return 0;
 }
but every time I run the program my output file just returns nonsense and doesn\'t do what I want it to, I have a feeling it has something to do with my While loop. If you could please tell me exactly what I need to add to this and why to make it run correctly I would greatly appreciate it thank you!
Solution
Opening file logic is wrong in your code, apart from this; remaining syntax is fine. Here I am attaching working and modified code.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
 #include<stdlib.h>
 #include <fstream>
 #include <string>
 using namespace std;
int main()
 {
 const char NEWLINE = \'\ \';
 const char TAB = \'\\t\';
 const char COMMA = \',\';
 char input_char = 1;
 string line;
 ifstream infile(\"withcommas.txt\");
 if (infile.fail())
 {
 cerr << \"error opening withcommas.txt\ \";
 exit(-1);
 }
 ofstream output(\"withoutcommas.txt\");
 if (output.fail())
 {
 cerr << \"error opening withoutcommas.txt\ \";
 exit(-1);
 }
 while (getline( infile, line )){
 for(int i=0;i<line.length();i++){
        if(line[i] != \',\'){ //filtering comma
            output<<line[i];
        }
    }
    output<<\"\ \";
 }
 infile.close();
 output.close();
 return 0;
 }


