Thread: Save/Load function not working :(

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Save/Load function not working :(

    I'm writing a program that keeps a text file which holds 10 lines. On each line will be a time, and a filename. The program will run in the background and at the specified time will play the sound file. I'm trying to load times from a text file into a variable. Here is my attempt, but nothing goes into the variable &times.

    Code:
    char* times[11];
    
    void SetTimes()
    {
    	int x=0;
    
    	for(x=0; x<=10; x++)
    	{
    		times[x] = "a";
    	}
    }
    
    void LoadTimes()
    {
    	ifstream load;
    	load.open("times.txt");
    	//FILE* File;
    	//File = fopen("times.txt", "rb");
    	//if(File != NULL)
    	    //MessageBox(NULL, "works", "test", MB_OK);
    
    	for(int x=0; x<=10; x++)
    	{
    		load>>times[x];
    		//fread(&times, sizeof(char*[11]), 1, File);
    	}
    
    	//fclose(File);
    	load.close();
    }
    SetTimes works fine, of course, but not LoadTimes. After LoadTimes is run, the variable times is still empty.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    It may be helpful to set the marker to the beginning of the document. try using seakg(NULL, ios::beg);
    i believe that's how it works.

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    my experience

    I get execution errors when running your code(using DevC++). Maybe the erros is because times[x] returns a pointer, and the compiler donīt know how overload the >> operator to read pointers from a file. Iīm not sure...
    Any ideas?
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    I don't actually want "a" in times. I just was doing that to test stuff. I want an array of strings which times is, right? I can't try the code right now, but the code you showed should do what I want?

    I want strings like "12 00 00" to be in one slot of the array. See, it's like hour, minutes, seconds.

  5. #5
    Cat
    Guest
    But you're never actually allocating space for any strings at all. You allocate space for 11 sting POINTERS, in this line:

    char* times[11];

    but that only allocates space to store the addresses of the strings. You allocate nothing for string data.

    SetTimes() works because you force each pointer to point to the same location in memory, which is somewhere in a data segment that contains the literal "a\0".

    In your loop, you'll need to allocate space for each string, and you need to free that at some point.

    Somewhere, you need a loop like this:

    Code:
    for(int x=0; x<=10; x++)
    {
       times[x] = new char[50];
    }
    (The above code assumes you limit lines to 50 characters; if you need more, make it bigger. You don't want to overflow your buffer.)

    And somewhere else, when you're done with the strings, clean them up:

    Code:
    for(int x=0; x<=10; x++)
    {
       delete[] times[x];
    }

  6. #6
    Cat
    Guest
    Of course, Salem's method will work, too. (The only difference is where the data is actually stored; stack vs. heap). I prefer heap variables because I've programmed for WinCE on the StrongARM, which (either because of the compiler or the underlying hardware) has a limited stack space to work with. On a machine in which stack space is not an issue, either works just as good.

    In any event, your problem is that you aren't creating any space to put the strings themselves.

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Ah, thank you so very much Salem and Cat. I appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM