File write-read problem and some questions

Hi,
I am trying to write a simple c++ script to learn more about file manipulation. First script is adding some info a file as a structure and second one is reading from the file. However when i try to read from my file it always shows the found values twice. I tried lots of things but couldnt find out where i am doing wrong. Either adding values twice or reading them twice. I appreciate if someone can lead me to the right direction. Here are my 2 scripts.

This one is used to write to the file
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
#include <fstream>
#include <iostream>
#include <cstring>
//#pragma hdrstop

using namespace std;
struct info
    {
    char name[50];
    int yas;
    };     
int main()
{       
      
    char Buffer[512]; 
    string newBuffer;
    int InputLength = atoi( getenv("CONTENT_LENGTH") ); 
    fread( Buffer, InputLength, 1, stdin ); 
    cout<<"content-type: text/html"<<endl;
    cout<<""<<endl;
    //html kodu
    cout<<"<html><head>"<<endl;
    cout<<"<title>ORNEK CGI UYGULAMASI</title>"<<endl;
    cout<<"<body>"<<endl;
    newBuffer = static_cast<string>(Buffer);
    free (Buffer);
    int loc = newBuffer.find_last_of('&');
    string submittedquery = newBuffer.substr(0, loc);
    int stringboyutu = submittedquery.length();
    int loc2 = submittedquery.find('&');
    string ad = submittedquery.substr(0, loc2);
    string yas = submittedquery.substr(loc2+1, stringboyutu);
    int adboyutu = ad.length();
    int yasboyutu = yas.length();
    int adbaslamayeri = ad.find('=');
    int yasbaslamayeri = yas.find('=');
    string tamad;
    tamad = ad.substr(adbaslamayeri+1, adboyutu);     
    string tamyas = yas.substr(yasbaslamayeri+1, yasboyutu);
    cout<<"Adiniz: "+tamad<<endl;
    cout<<"<br>"<<endl;
    cout<<"Yasiniz: "+tamyas<<endl;
    cout<<"</body>"<<endl;
    cout<<"</html>"<<endl;
    info myinput;
    int TempNumOne=tamad.size();
    for (int a=0;a<TempNumOne;a++)
        {
            myinput.name[a]=tamad[a];
            if (a==(TempNumOne-1)){
                    myinput.name[TempNumOne] = '\0';
                    }           
        }
    //menu1.name = static_cast<char>(tamad);
    myinput.yas = atoi(tamyas.c_str());
    ofstream file;
    file.open("bilgi.dat",ios::app | ios::binary );                 
    file.write((char*)&myinput,sizeof(info));
    file.close(); 
    return 0;
}


This one is used to read from file
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
#include <cstdlib>
#include <fstream>
#include <iostream>
//#pragma hdrstop

using namespace std;

struct info
    {
    char name[50];
    int yas;
    };  
int main()
{      
    
    char Buffer[512]; 
    string newBuffer;
    int InputLength = atoi( getenv("CONTENT_LENGTH") ); 
    fread( Buffer, InputLength, 1, stdin ); 
    cout<<"content-type: text/html"<<endl;
    cout<<""<<endl;
    //html kodu
    cout<<"<body>"<<endl;
    cout<<"Bulunan kullanici:<br>"<<endl;
    int position;
    newBuffer = static_cast<string>(Buffer);
    free (Buffer);
    int loc = newBuffer.find('&');
    string ad = newBuffer.substr(0, loc);
    int adbaslamayeri = ad.find('=');
    int adboyutu = ad.length();
    string tamad = ad.substr(adbaslamayeri+1, adboyutu); 
    info myinput;
    fstream file;
    file.open("bilgi.dat",ios::in|ios::binary);
    file.seekg(0,ios::end);//dosya sonuna git
    int endposition = file.tellg();
    file.seekg(0);
    int totalrecords = endposition/ sizeof(info);
    for (int rec_no =0; rec_no<totalrecords; rec_no++){
        position = rec_no*sizeof(info);
        file.seekg(position);
        file.read((char*)&myinput,sizeof(info));
        file.seekg(0);
        if (!file.good ())
        break;
        if ((static_cast<string>(myinput.name)) == (tamad+"\0")){
            cout<<"Ad:"<<endl;                                
            cout<<myinput.name<<endl;
            cout<<"Yas:"<<endl;   
            cout<<myinput.yas<<endl;
            cout<<"<br>"<<endl; 
        }
    }
    cout<<"</body>"<<endl;
    cout<<totalrecords<<endl;  /*wrote this to see total records, and it shows the records twice */       
    return 0;
}


There may be lots of errors in my code btw i am no expert :)
Topic archived. No new replies allowed.