no errors but it doesn't work :(

Hi all, I posted this in the Beginners section but got no response.

original thread http://www.cplusplus.com/forum/beginner/59633/

I've made a program to read in a .csv file, the user selects the headers they want extracted and written to another file. e.g. if the original csv has 57 headers, the user might only be interested in columns 2,3,4,6,8,10,27,

I've managed to extract the data from the chosen columns and display them in the cmd window.
I'm storing it as a string. so if the input csv is...

A, B, C, D, E, F,
11, 21, 31, 41, 51, 61,
12, 22, 32, 42, 52, 62,

and the user selected columns 1,3,5, the output is ...

11, 31, 51,
12, 32, 52,

Here's the section of code below. I've commented out OutputFile as it wasn't working. I copied the example from http://www.cplusplus.com/doc/tutorial/files/
but even this doesn't work... it compiles fine and runs no probs so i'm stuck as to where the problem is... "example.txt" isn't created.


this snippet is some way into the progam
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
    //ofstream OutputFile; //open output file
    //OutputFile.open ("DAPout.txt"); //create output file
    
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    
    cout<<"Output file created?";
    system("PAUSE");
    
    read.open (argv[1]);
     if (read.is_open()) { //if it's open
        getline (read,line);  //skip the first line, they are headers
        getline (read,line);  //skip the second line, they are units
        while ( read.good() )  //while it's still open
        {
        getline (read,line);  //read the data from 'read' obj, store in 'line'
        
        DataStr = DataFromLine(line, ColsToExport, HEADER_LENGTH, DelimChar, NumToFetch);
        cout<<"Concat Str " <<DataStr<<"\n";
        //OutputFile << DataStr; //write extracted data
        
        }
     } else {
        cout<<"Failed to read input file \n";  //quit if can't read an input file
        return EXIT_SUCCESS;
     }
 read.close();         //close the inputfile
 //OutputFile.close();   //close the output file  



So I went back and experimented a bit...I enter a test string (strin) at the start of the program, create the file and write the test string if no input file is present, this works. However if I access the input file and try to write to the test file after doing this it doesn't work.

I've commented the lines that do and don't work
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
34
35
36
37
38
39
40
41
 //input a test string to write to file
 string strin;
 getline(cin,strin);
 ofstream OutputFile;            //open output object
 OutputFile.open ("DAPout.txt"); //create output file    

 read.open (argv[1]); //Opens the file drag'n'dropped onto the .exe
 
//==========================================================
// This section is about extracting the header data
//==========================================================
 if (read.is_open()) { //if it's open
       getline (read,line);  //read the data from 'read' obj, store in 'line'
       
       //user enters the char used as delimiter
       cout<<"Enter Delimiter char :";
       cin>>DelimChar;
      
       //find the length of the header to deal with differnt files
       HEADER_LENGTH = FindLineLength(line,DelimChar);
       cout<<"Number of headers in the file = "<<HEADER_LENGTH<<"\n";
 
       found=line.find(DelimChar);  //find the first delimiter
       delim[0]=found;     //store the first location of delimiter
       
       for (int n=1;n<=HEADER_LENGTH;n++) {
           found=line.find(DelimChar,found+1); //finds the next delimiter
           delim[n] = found;            //logs the locations
           if (found>line.size()) break; //exit loop if loc is > length of input line
       }
 } else {
      cout<<"Failed to read input file \n";  //quit if can't read an input file
      OutputFile << strin;   //this works, DAPout.txt is created
      OutputFile.close();   //this works, DAPout.txt is created
      return EXIT_SUCCESS;
 }                                 // end of if (read.is_open())
 
read.close();         //close the input file, don't need it at the mo
 
//     OutputFile << strin;  //this doesn't! DAPout.txt is not created
//     OutputFile.close();   //this doesn't! DAPout.txt is not created 


Any ideas?
Topic archived. No new replies allowed.