Problem with .dat file...

This is a discussion on Problem with .dat file... within the C++ Programming forums, part of the General Programming Boards category; Hey guy's... Anyway's, none of my coder friend's are online and i really need some help.. When I start my ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    Question Problem with .dat file...

    Hey guy's...

    Anyway's, none of my coder friend's are online and i really need some help.. When I start my program, it create's a .dat file.. The file is supposed to store all the information that I write in my program.. Problem is that once I close the program and re-open it it's not there.. The .dat file is blank (0Kb's) so it has to be some stupid error i have done.. If you can show me how to fix it I would really appreciate it.. Thank's...

    Pic of the program when ran:
    http://img.photobucket.com/albums/v4...iosProgram.jpg

    Code:
    /*Nitto Revolution Ratio's*/
    
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <fstream>
    
    // This structure defines the car stats.
    struct SCar
    {
    	char car[30];
    	char user[30];
    	float gear1, gear2, gear3, gear4, gear5, gear6, final, et;
    };
    
    int menu()
    {
    	std::cout << "\n\n\n-------------------------------------------------\n";
    	std::cout << "What would you like to do?\n1) Add a car.\n2) View all cars.\n0) Quits\n";
    	std::cout << "-------------------------------------------------\n\n";
    	int selection;
    	std::cin >> selection;
    	return selection;
    }
    
    SCar CreateCar()
    {
    
    	SCar car;
    	
    	std::cout << "\nCar name?: ";
    	std::cin.ignore();
    	std::cin.getline(car.car, 29);
    
    
    	std::cout << "User?: ";
    	//std::cin.ignore();
    	std::cin.getline(car.user, 29);
    
    	std::cout << "Gear one?: ";
    	std::cin >> car.gear1;
    
    	std::cout << "Gear two?: ";
    	std::cin >> car.gear2;
    
    	std::cout << "Gear three?: ";
    	std::cin >> car.gear3;
    
    	std::cout << "Gear four?: ";
    	std::cin >> car.gear4;
    
    	std::cout << "Gear five?: ";
    	std::cin >> car.gear5;
    
    	std::cout << "Gear six?: ";
    	std::cin >> car.gear6;
    
    	std::cout << "Final?: ";
    	std::cin >> car.final;
    
    	std::cout << "ET?: ";
    	std::cin >> car.et;
    
    	return car;
    }
    
    void printcar(SCar* pCar)
    {
    	printf("%s::%s \n\t%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f \n", pCar->car, pCar->user, pCar->gear1, pCar->gear2, pCar->gear3, pCar->gear4, pCar->gear5, pCar->gear6, pCar->final, pCar->et);
    }
    
    
    
    int main()
    {
    	std::cout << " /-------------------------------------\\ \n";
    	std::cout << "<------- Nitto Revolution Ratio's -------> \n";
    	std::cout << "<-------  Coder: ArtificialAimer  -------> \n";
    	std::cout << " \\-------------------------------------/ \n";
    	int i, totalcars = 0;
    	SCar* pCars = 0;
    
    	std::fstream file;
    	file.open("Ratios.dat", std::fstream::out | std::fstream::binary);
    	file.seekg(0);
    	file.read((char*)&totalcars, sizeof(int));
    	pCars = new SCar[totalcars];
        file.read((char*)pCars, sizeof(SCar) * totalcars);
    
    	while((i = menu()) != 0)
    	{
    		switch(i)
    		{
    		case 1:
    			{
    				
    				SCar* pCarsTemp  = new SCar[totalcars+1];
    				memcpy(pCarsTemp, pCars, sizeof(SCar) * totalcars);
    				delete[] pCars;
    				pCars = pCarsTemp;
    				pCars[totalcars] = CreateCar();
    				totalcars++;
    
    				file.seekp(0);
    				file.write((char*)&totalcars, sizeof(int));
    				file.write((char*)pCars, sizeof(SCar) * totalcars);
    				break;
    			}
    		case 2:
    			{
    				for(int j = 0; j < totalcars; ++j)
    					printcar(&pCars[j]);
    
    				
    
    				break;
    			}
    		default:
    			break;
    			//nothing
    		}
    	}
    
    	file.close();
    	return 0;
    
    
    }

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    	
            file.open("Ratios.dat", std::fstream::out | std::fstream::binary);
    	file.seekg(0);
    	file.read((char*)&totalcars, sizeof(int));
    You are setting open to
    Code:
    std::fstream::out
    then you are reading it in
    Last edited by JoshR; 06-06-2005 at 02:22 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Okay, I am kinda new to C++, so what exactly does that mean? Do I need to change anything? Sorry, but Im not sure what your trying to tell me to do...
    Last edited by DoraTehExploda; 06-06-2005 at 02:47 PM.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    well you told your program to open the .dat file for output. Then you tried to read in the file which shouldnt work because there is nothing in the file (it has just been made). Then you used the ammount of total cars (currently 0 because file has just been made), and it created an array of [0] lenght.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Well, how should i rewrite it?

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    I was just looking through your code in my compiler and noticed that if you want to keep adding to the .dat file you should add this to your open parameters:

    Code:
    std::fstream::app
    Also, I would check if there is an existing .dat file first, if there is than read in the number of cars and their individual data.

    If it doesnt exist then make the .dat file.
    Last edited by JoshR; 06-06-2005 at 09:58 PM.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Sorry im not that familiar with some of the C-syntax you are using

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Well, i have no idea how to do this stuff.. This was supposed to be a simple project but it's gottin really complicated for me...

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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