Not able to understand why I can't read from a file

0 down vote favorite


I am trying to read from the file where 3 columns are present; Turn, R1[integer value] and R2[integer value]. I am supposed to create a file for each "turn" I encounter with the name as that of the turn [e.g. for TurnII, it creates Turn2.txt]and then read the second and third column entry for the corresponding turn and then look for all the values between the second and third column, inclusive, from a different file and do some operations on that file.

The problem is that my code reads the turn, but I am not able to read the next character i.e the first character of the second column. I tried to do every possible thing even removed loops and goto statements and wrote a big code. But I don' understand where the fstream object points after reading T,U,R,N and the index value e.g. I or I,I.

This is the link to the file I am trying to read. http://goo.gl/EIfFrQ

I am giving you the function which does this task.

void Tempread()
{
char c2[10];
int qw;
int i;
double ret;
fstream f("temp.txt",ios::in);
while(!f.eof())
{

f>>c2[0];
if(c2[0]=='T')
{
f>>c2[1];
if(c2[1]=='u')
{
f>>c2[2];
if(c2[2]=='r')
{
f>>c2[3];
if(c2[3]=='n')
{
f>>c2[4];
if (c2[4]=='I'||c2[4]=='V')
{
f>>c2[5];
if (c2[5]=='I'||c2[5]=='\''||c2[5]=='V')
{
f>>c2[6];
if (c2[6]=='I'||c2[6]=='a'||c2[6]=='b'||c2[6]=='\'')
{
f>>c2[7];
if (c2[7]=='I') //VIII
{
fstream f1("Turn8.txt",ios::app|ios::out);
//residue1(f,f1);
f1.close();
}
else
{ //f.seekg(-1,ios::cur);
if (c2[6]=='\'') //II'
{ fstream f1("Turn2'.txt",ios::app|ios::out);
f1.close();
}
else if (c2[6]=='b') //VIb,
{ fstream f1("Turn6b.txt",ios::app|ios::out);
f1.close();
}
else if (c2[6]=='a') //VIa
{ fstream f1("Turn6a.txt",ios::app|ios::out);
f1.close();
}
}

}
else // II/I'/IV
{
f.seekg(-2,ios::cur);
if (c2[5]=='I') //II
{
qw=0;
while(qw!=2) //Seraches for range of residue
{
getline(f,str[qw],'\t');
value[qw] = atoi(str[qw].c_str());
qw++;
}
cout<<str[0]<<'\t'<<str[1];

}
else if (c2[5]=='\'') //I'
{ fstream f1("Turn1'.txt",ios::app|ios::out);
f1.close();
}
else if (c2[5]=='V') //IV
{
qw=0;
while(qw!=2) //Seraches for range of residue
{
getline(f,str[qw],'\t');
value[qw] = atoi(str[qw].c_str());
qw++;
}
fstream f1("Turn4_Phi.txt",ios::app|ios::out);
}
}
}
else //I
{
f.seekg(-3,ios::cur);
qw=0;
while(qw!=2) //Seraches for range of residue
{
getline(f,str[qw],'\t');
value[qw] = atoi(str[qw].c_str());
qw++;
}
}
}
}
}
}
}
}
f.close();
}
Ok, I had a quick look at your code, but all of those nested if statements make it look overly complex for such a relatively simple input file.

I've not attempted to reproduce all of the functionality of your code, but this is how I'd suggest reading from the file. All I do here is to print it out. But from this sort of basis you could start to add logic to act depending upon the values of the data which was read.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ifstream fin("temp.txt");
    
    string turn;
    int r1, r2;
    
    // ignore the first line which contains column headings
    fin.ignore(1000, '\n');
    
    // read each line of the file and print it out
    while (fin >> turn >> r1 >> r2)
    {
        if (turn.size() > 4)
        {
            string t1 = turn.substr(0,4); // should always be "Turn"
            string t2 = turn.substr(4);   // extract the important part of turn
           
            cout << setw(8) << t1 
                 << setw(8) << t2
                 << setw(8) << r1
                 << setw(8) << r2
                 << endl;     
        }
        else
        {
            cout << "unexpected turn: " << turn << endl;
        }
    }

    return 0;
}

Output:
    Turn      II     138     141
    Turn       I     188     191
    Turn      IV     189     192
    Turn      IV     190     193
    Turn      IV      54      57
    Turn      II     138     141
    Turn       I     188     191
    Turn      IV     189     192
    Turn      IV     190     193
    Turn      IV      54      57
    Turn      IV      55      58
    Turn      II     138     141
    Turn       I     188     191
    Turn      IV     189     192
    Turn      IV     190     193


Last edited on
Topic archived. No new replies allowed.