Endless loop when read and write

Dear all,

I have a data file from radio astronomy observations. Unfortunately, all observations have been written to one file.

The file looks like this

SCAN = 1
SOURCE = 190_0
DATE-OBS = 10/27/11
LST = 11:56:15
L = 190.00000000
B = 0.0000000
AZ = 280.13165283
EL = 17.9822598
TC = 10.00
TSYS = 91.00
DUR = 200
NCH = 1024
VELDEF = LSR RADIO
NU = 1420.000
V = 86
DNU = 0.00488281
DV = 1.031580
DATA
271.857
266.060
265.558
265.030
265.186
265.110
264.159
264.301
264.702
262.905
263.423
....
END
Next header and values.

I am trying to extract each and every one of those parts into seperate files.

The code I have written so far is

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <istream>

using namespace std;

/*double divide_by_two(double brightness, scan_number)
{	
    double corrected_brightness;
    
    corrected_brightness= (brightness/(2*scan_number));
    
}
*/
int main()

{
  
  string inputfile;
  
  cout << "input file?";
  cin >> inputfile;
  
  ifstream input;
  
  input.open (inputfile);
  
  
  do{
    string line;
    getline(input, line)
    if(line= "SOURCE=")
    {
    string outputname;
    line=outputname;
    ofstream output;
    
    output.open(outputname ".txt")
 
    }
    else if(line="DATA")
    {
      do{
	
	
      output >> input;
      
     
      }while(line!= "END")
      
output.close
    }
    
  }while(!input.eof)
   

  return 0;
}
    
    
    


But it doesn't compile because of the output not being declared. Also how would I go about dividing the values by 2(scan number=)

Any help would be greatly appreciated.




Last edited on
I see several possible errors:

Instead of output.open(outputname ".txt") should be output.open(outputname + ".txt");
(end your statement with ;, correct also line 33).

= is an assignment operator. For comparison use string.compare() or ==,
Instead of if(line= "SOURCE=") maybe if(line == "SOURCE=") is more appropriate.

http://www.cplusplus.com/reference/string/string/compare/
Dear all,

thanks for the string compare pointer. It got me on the right track with str.find(str2). I have so far managed to ge the programm to produce the correct files but I have a porblem with the data output. I have only managed via a counter of the amount of line to copy the entire file but I have been unable to only get the numbers in between DATA and END written into the file.

I think the correct condition would be a

if{string data is found}
do{
input ->output
}while(string does not equal END)

The above gives an endless loop which just writes into the same output file over and over. Is there a way to give the break command a trigger in the form of a string being read in?

Here is the code I have at the moment.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

int main () {



  string inputfile;
  cout << "input file?";
  cin >> inputfile;
 
  fstream input;

  input.open (inputfile.c_str());


  
  
  
   do{
      
     
    string line;
    getline(input, line);
    string source ("SOURCE = ");
    string data   ("DATA");
    string finish ("END");
     
    //while(line.find(finish) == string::npos){
    if(line.find(source)!= string::npos)
    {
      string outputname;
	
      outputname.assign(line,9,10);   
   
       
    ofstream output;
   
    output.open(outputname.c_str());
    
      
   
    cout << "first loop\n";
      
    //for(line.find(data) !=string::npos;)
    //{
  
	do{
	string temp;
	
	getline(input,temp);
	
	
     output << temp;
     output << endl;

     
      
    
     
    
      }while(line.find(finish) == string::npos);
    
         output.close();
     cout << "second loop\n";
   }
    
   
  }while(!input.eof());
  

  input.close();

  return 0;

}



Thanks for any replies
Topic archived. No new replies allowed.