I am writing a program in which i will be using 2 functions, one for writing integers into a text file & second for reading them back in integer form. while i did not faced in writing int say 100 numbers into a text file. i am stuck on reading from it. my program requirement is that, i want to read only one integer at a time when i call read function & when i call read function next time, it should return next number from the file. i think their can be 2 ways to solve this problem. first is that once i have read a number from the file, that number should be deleted & when i call read function next time, it will return next number from the file. in second method i can use pointer to keep track of them. i am not sure how to do it. please help.

code is ..........

int writefile(char *str, int size)
{
    uint32_t c;
    ofstream outfile;
    outfile.open("output.txt");
    for(int i=0;i<size;i++)
    {
        c = hashlittle(str, strlen(str), 0);
        printf("%.8x\n", c); 
        outfile<<c<<endl;
        sprintf(str, "%u", c);
    }
    outfile.close();
    return 1;
}

uint32_t fetchfile(int ctr)
{
    uint32_t hash, array[10];;
    ifstream myfile ("output.txt");
    myfile.is_open();
    for(int i=0;i<10;i++)
    {
        myfile>>array[i];
        printf("value fetched from file: %.8x\n", array[i]); 
    }
    hash = array[ctr];
    printf("Current value fetched from file: %.8x\n", hash);
    myfile.close();
    return hash;
}

int main()
{
    //uint32_t c;
    //int ctr =0;
    char str[80];
    cout<<"enter string: ";
    cin.get(str,80);
    cout<<endl;
    writefile(str,10);
    fetchfile(0);
    return 0;
    /*
    ofstream outfile;
    outfile.open("output.txt");
    for(int i=0;i<10;i++)
    {
    c = hashlittle(str, strlen(str), 0);
    printf("%.8x\n", c); 
    outfile<<c<<endl;
    sprintf(str, "%u", c);
    }
    uint32_t hash, array[10];;
    ifstream myfile ("output.txt");
    myfile.is_open();
    for(int i=0;i<10;i++)
    {
    myfile>>array[i];
    printf("value fetched from file: %.8x\n", array[i]); 
    }
    hash = array[ctr];
    printf("value fetched from file: %.8x\n", hash);
    myfile.close();
    */

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.