explaining of codes

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
can someone explain the function below to me
void readFile()
{
    ifstream read("bs.txt");
    ofstream fout("tmp.txt");
    int roomNo, levelNo, time, duration,x;
    char date[10],subject[30],lecturer[30],temp[30],c;
    
    read>>x; 
    read.seekg(1,ios::cur); 
    read.get(c);
    while(read)
    {
        if(c!=10)
            c-=x*10;
        fout.put(c);
        read.get(c);
    }
    read.clear();
    fout.close();
    read.close();
    read.open("tmp.txt");
    
    //receiving values from file and storing in variables
    read.getline(temp,30,':');
    ID = atoi(temp);
    read.getline(temp,30,':');
    roomNo = atoi(&temp[5]);
    read.getline(temp,30,':');
    levelNo = atoi(&temp[6]);
    read.getline(temp,30,':');
    strcpy(date,temp);
    read.getline(temp,30,':');
    time = atoi(temp);
    read.getline(temp,30,':');
    duration = atoi(temp);
    read.getline(temp,30,':');
    strcpy(subject,temp);
    read.getline(temp,30,'\n');
    strcpy(lecturer,temp);
    
    while(read)//while data is read from file
    {
        add(ID,roomNo,levelNo,time,duration,date,subject,lecturer);
        read.getline(temp,30,':');
        if(!read)//if there is file input error
            break;
        ID = atoi(temp);
        read.getline(temp,30,':');
        roomNo = atoi(&temp[5]);
        read.getline(temp,30,':');
        levelNo = atoi(&temp[6]);
        read.getline(temp,30,':');
        strcpy(date,temp);
        read.getline(temp,30,':');
        time = atoi(temp);
        read.getline(temp,30,':');
        duration = atoi(temp);
        read.getline(temp,30,':');
        strcpy(subject,temp);
        read.getline(temp,30,'\n');
        strcpy(lecturer,temp);
    }
    read.close();
    remove("tmp.txt");
}

Last edited on
Please use the code tags and intuitive indentation. See http://www.cplusplus.com/articles/jEywvCM9/

Try to break the code into smaller, logical blocks. Do you have any guesses yourself?

Furthermore, your other thread is an essential part of this. http://www.cplusplus.com/forum/general/138873/
They should be together. (You should assume that whoever reads that other thread, has not seen this thread and vice versa.
yea it's a function to read in info and then write to files with users input but i get how it works
The code seems to do "silly" things and might not even work properly.

However, there are clearly two unrelated operations, so we could rewrite:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void readFile()
{
    ifstream read("bs.txt");
    ofstream fout("tmp.txt");
    deobfuscate( read, fout );
    fout.close();
    read.clear();
    read.close();

    
    ifstream data("tmp.txt");
    // for each line in data
    //    extract values from line
    //    add(ID,roomNo,levelNo,time,duration,date,subject,lecturer);
    data.close();

    remove("tmp.txt");
}

However, the use of (slow) temporary file is strange, because one could use stringstream, or even deobfuscate one line at a time.
Topic archived. No new replies allowed.