Thread: Working with files v2

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Working with files v2

    Hello, I posted a thread regarding files not long ago, but I came across with a few more challenges along the way. The code is related to the slidding puzzle game.

    Let's say I got the following .txt file:

    Code:
    1 2 4
    3 6 5
    8   7
                 John   15 M     55
               William  13 F    120

    The board that you see is an unsolved puzzle that "John" and "William" managed to finish in, respectively, 55 and 120 seconds.

    After the user finishes a puzzle he must enter his name ("John"), age("15) and gender ("M"). The program will write the information to the file and the time the user took to solve the puzzle ("John 15 M 55").

    The program itself is supposed to make a top 5 with the best times and the info regarding the same. This means that whenever there's a new time, THE NEW TIME has to be compared with the ones already in that file.

    Ok so, in the thread I talked about earlier I managed to retrieve the times from the file and put them in a vector and sort them out with the new time. Although there was nothing but the times themselves in the vector.

    My problem is: How to retrieve the times out of the file, sort them out with the new time and reorganize the file with the respective user info and time.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should know by now you need to show what you've tried. Where's the code?

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'm really lost at this point, need some guidelines to execute any code at all.
    The one you helped me with does not allow to retrieve data when there's anything but integers/numbers in the file.

    I suppose I'm required to retrieve the entire line ("John 15 M 55") through a stringstream and then divide it into other stringstreams so I can work with the times (and put them in a vector).

    >getline(file, line)
    This gets the whole line, and I'm lost beyond this point.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    >getline(file, line)
    This gets the whole line, and I'm lost beyond this point.
    You mentioned stringstream did you look up that class's documentation?

    Once you create an instance of the stringstream it work the same way cin works.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I made some code which gets the lines from the file. Yet it does not seem to be working properly.

    Code:
    vector<string> read_top_ten_from_txt (int board_size, int puzzle_number)
    {
            vector<string> top_ten;
    		string filename, line;
            
     
            /* if file exists, read top 10 from it */
            if (Checks_for_file("puzzle_3x3_001.txt"))
            {
                    /* opening input file stream */
                    ifstream read_from_txt ("puzzle_3x3_001.txt");
     
                    for (int i = 0; i < board_size + 1; i++)
                    {
                            getline(read_from_txt, line);
                    }
                    while (true)
                    {
                            getline(read_from_txt, line);
                            if (line[0] == '\0')
                            {
                                    break;                         
                            }
                            top_ten.push_back(line);
                    }
            }
            else
            {
                    cout << "Error: Couldn't open file (" << "puzzle_3x3_001.txt" << ").";
            }
           
            return top_ten;
    }
    
    
    int main()
    {
    	vector<string> vec = read_top_ten_from_txt(3,001);
    
    
    	for (int i = 0; i < vec.size(); i++)
    	{
    		cout << vec[i] <<endl;
    	}
    
    
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Doesn' seem to work properly isn't a good problem description.
    Without knowing the contents of the file it's even worse.
    Anyway if the file doesn't contain more then board_size +1 lines not much will be pushed onto top_ten
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with png files
    By rodrigorules in forum C Programming
    Replies: 5
    Last Post: 05-12-2010, 10:09 AM
  2. Working with files in C
    By fmchrist in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2005, 02:28 PM
  3. working with files in c
    By fmchrist in forum C Programming
    Replies: 4
    Last Post: 12-27-2005, 02:03 PM
  4. working with files
    By kristy in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 04:49 AM
  5. working with files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-05-2001, 01:58 AM