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

#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

#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;
}

First program:
line 18: fread() doesn't work very well with stdin because it doesn't terminate the input with the string null terminator. That's why everyone calls fgets().

Since you are writing a c++ program you can do away with fgets() and fread(), just use the functions in std::cin and std::string std::getline(newBuffer, std::cin); line 26: you can only call free() on pointers that have been allocated with malloc(). Since Buffer has not been allocated with malloc() the free() call on line 26 will fail. Your compiler should have complained about that, assuming you attempted to compile that code at all.

line 28: you are assuming that the find() on the previous line succeeded. What will that do if it failed? You need to add some error checking here.

when i use

std::getline(newBuffer, std::cin);

it says C:\xampp\cgi-bin\main2.cpp no matching function for call to `getline(std::string&, std::istream&)' :(

You need to include <string> header file.

it is already included mate

it is already included mate

Look again, mate...

i used

int InputLength = atoi( getenv("CONTENT_LENGTH") ); 
    fgets(Buffer, InputLength+1, stdin);

and it solved the problem, thanks very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.