confused!

This is a discussion on confused! within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, 1) as per requirement my prog.should handle a file with no records ,1 record or many records..... I ...

  1. #1
    Unregistered
    Guest

    confused!

    Hi everybody,

    1) as per requirement my prog.should handle a file with no records ,1 record or many records.....

    I am not sure how to handle a file with no records.....in it.

    2) Now as per my code, if there are no records then too value of tmprec(i.e. Number of current records in the file)increases to 1...
    but actually it should remain 0 ........


    Pls. let me know where I am going wrong......Thanks a lot.



    my code:

    Code:
    int adatabase(competitor database[], int &tmprec , int &tempID)
    {
         int i =0;
        //tmprec = Number of current records.
    
        ifstream inputFile("records.txt", ios::in );
        
        tempID=  atoi(readFileLine(inputFile).c_str());// latest ID
       
     //if inputfile is empty ....tmp rec == 0;????I am not sure how to approach this//
    		
    
       while(!inputFile.eof() && tmprec < DATABASESIZE)
       {
    			
         database[tmprec].competitorName= readFileLine(inputFile);
         database[tmprec].ID = atoi(readFileLine(inputFile).c_str());
         database[tmprec].compGender = readFileLine(inputFile);
        database[tmprec].birthDate.day= atoi(readFileLine (inputFile).c_str());
          database[tmprec].birthDate.month = atoi(readFileLine(inputFile).c_str());
        database[tmprec].birthDate.year = atoi(readFileLine(inputFile).c_str());
       tmprec= tmprec++; 
    
       }
    
    
    return tmprec;
    return tempID;
    
    }

  2. #2
    Unregistered
    Guest

    Unhappy Help Me

    12 reads,but still no reply??help this beginner out.....
    Btw, why cant I log in as butterfly........I am already registered as butterfly.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    What is 'readFileLine'? please include all necessary components needed to analyze this.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    tellg()

    inputFile.tellg(0, ios:end);

    Returns the number of bytes to the end of the file. No records, no bytes, hence, tmprec = 0. (I leave the code to you.)

    P.S.
    A lot of folks read these threads and, perhaps, don't know the answer to a question or don't want to be bothered.

    It's not good form to "demand" an answer from the people out here simply based on the number of times that your post has been read.

    The cold reality: Your problem is your problem, not ours.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    7

    sorry.....

    Sorry.....If I have caused any trouble.......But I am genuinely not demanding......the way this board helps the newbies is impressive ,so I believe , I too will get better response....My Q is also not based on any advanced topic , also i am not asking for the entire code..... unlike other guys....I am just asking to point out where my logic is going wrong.....
    Anway ur right in one way.......

    thanks achaacha ..for showing interest ...

    this is the readFileLine funcn
    Code:
    string readFileLine(ifstream &inputFile)
    {
        char buffer[SIZE];
        inputFile.getline(buffer, SIZE, '\n');
        return buffer;
    }

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    readFileLine(inputFile).c_str());// latest ID

    Don't think you can do this because c_str() is a std string specific function. You can't do this on a char array.


    inputFile.getline(buffer, SIZE, '\n');

    Watch out with this because if someone entered SIZE characters, it would over run the buffer because the null terminator is added to the end.

    Start with that and see how far you can go.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    7

    Unhappy still not working.....

    Hi,

    Skipper as per ur suggestion......I just tried to implement the tellg()funcn like this....

    Code:
    int x =inputFile.tellg();
    cout<<x<<endl;
    I tried using that 0, ios::end inside the tellg function, but err is shown --- 'tellg' : function does not take 2 parameters
    the ouput is 2 , even if my text file is empty....can u enlighten me pls??thanks......

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    You're right.

    Bad information on my part and I apologize.

    What you need is:
    Code:
    inputFile.seekg(0, ios:end)
    (Got ahead of myself here!)

    This code places the pointer at the end of the file.

    Now, try your code.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    7

    Smile thanks...

    Thanks a lot Skipper......
    I was able to solve it...

    golfingguy4, ur right.....but I don have any alternative ,my teacher wants this function..
    no other way of accepting the contents are allowed...
    Also ID is a string rather than char array...so its working ....
    thanks anway for replying to this thread....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 04:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21