Reformatting a text file

I am getting large (several MB) spreadsheets in txt form with way more information than I need. It is predictable. So I want to load up my input file, put the first line into an array of chars, pick out the few relevant pieces into an output array, copy the output array to a separate output file, and loop through for each line in the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main(){

    char input[5459];
    char output[1321] = { 0 };//initalize all values to 0
    ifstream infile8("input.txt");//input file
    

    ofstream fout("output.txt");//output file

    int i;
    while (infile8.getline(input, sizeof(input))){
    //while  (!infile8.eof()){//I previously had this but was informed it was wrong. 
    //When I did have the above it ran infinitely, and the below cout printed "\277"
      //cout << input[2] << " \n";
      output[0] = input[18];//transaction status
      //I select a bunch more info from the arrays here like above
      i = 0;
      while (i <= 1320){
          fout << output[i];
          i++;
      }
      fout << '\n';

      //output = { 0 };//reset output file
      }

    return 0;
    }


When I tried it with a random small file (3 lines, a dozen chars of gibberish on each) it wrote +2 3 times in my output file. When I tried it with my large file, it never enters the loop.
Last edited on
put the first line into an array of chars

Why the array of char, why not use a std::string to get the entire line, then use a stringstream to parse that string?

It currently never enters the loop.

I suggest you check to insure that the files opened correctly.

Topic archived. No new replies allowed.