Strange error and impending sanity loss

This is a discussion on Strange error and impending sanity loss within the C Programming forums, part of the General Programming Boards category; Well, either you lost me somewhere along the very short road, or something else is not as it should be, ...

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    Well, either you lost me somewhere along the very short road, or something else is not as it should be, because I still can't get it to write properly.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Why not show your current attempt?
    I'm sure I can punch a few holes through it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    Hehe, I'm sure you can punch many, and you're welcome to as long as I can patch them up again.

    I didn't change much from the code I posted aside from the two you pointed out. Basically

    Code:
        if (fwrite(test, sizeof(test[0]), 11, fil) != 1){
    and

    Code:
        fread(highscore, sizeof(highscore[0]), 11, stream);
    Secondly, you tell fwrite to write the size of the type it points to, since all elements in an array are the same size, test[0] is the same size as test[1], etc.
    And that size is not necessarily the size of a pointer. Oh no. Undefined behavior right there.
    I'm not clear on what you meant with this, so I didn't change it. I thought declaring the size of the element is what I wanted to do.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Maer View Post
    Hehe, I'm sure you can punch many, and you're welcome to as long as I can patch them up again.

    I didn't change much from the code I posted aside from the two you pointed out.

    I'm not clear on what you meant with this, so I didn't change it. I thought declaring the size of the element is what I wanted to do.
    But that doesn't help us know what you've changed, from which version of what was posted, etc.

    As to the second, you want to declare the size of the element. Using sizeof(highscore) doesn't give you the size of the element, since highscore isn't an element, it's a pointer.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    But that doesn't help us know what you've changed, from which version of what was posted, etc
    Finally, something I can fix.



    highscore.h
    Code:
    #ifndef HIGHSCORE_H_
    #define HIGHSCORE_H_
    
    extern int highscore[];
    extern int test[];
    
    
    //void InsertScore(int *highscore);
    
    int SaveHighscore(int *test);
     
    void LoadHighscore(int *highscore);
    
    
    #endif /*HIGHSCORE_H_*/
    highscore.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include "highscore.h"
    
    // open file
    int SaveHighscore(int *test)
    {
        FILE *fil;
    
    
        fil = fopen("highscore.txt", "w");
        if (fil == NULL) {
            printf("Unable to open file\n");
            goto error;
            }
        fwrite(test, sizeof(test[0]), 11, fil);    
        if (fwrite(test, sizeof(test[0]), 11, fil) != 1){
            printf("Unable to write to file\n");
            goto error;
            }
        fclose(fil);
        
        return 1;
        
        error:
            return 0;
    }
    
    
    void LoadHighscore(int *highscore)
    {
        FILE *stream;
        int i;
        
        
        stream = fopen("highscore.txt", "r");
        
        
        if(stream==NULL) {
            printf("Error: can't open file.\n");
        }
      else {
        printf("File opened.\n");
      }
            
        fread(highscore, sizeof(highscore[0]), 11, stream);
        for(i=0; i<11; i++){
        printf("File read: %d \n", highscore[i]);
        }
        fclose(stream);
        
    }
    main.c
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <assert.h>
    #include "SDL.h"
    #include "SDL_ttf.h"
    #include "drawline.h"
    #include "triangle.h"
    #include "teapot_data.h"
    #include "sphere_data.h"
    #include "object.h"
    #include "text.h"
    #include "list.h"
    #include "highscore.h"
    
    (snip)
    
    
    
    int highscore[11] = {1,1,2,3,4,5,6,7,8,9,9};
    int test[11] = {2,2,2,2,2,2,2,2,2,2,2};
    
    (snip)
    
    int main(int argc, char **argv)
    {
        // Start game
        while (1) {
      
    
            
            SaveHighscore(test);
            LoadHighscore(highscore);

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    A few security addments, a few fixes and indentation fixing.

    Highscore.h:
    Code:
    #ifndef HIGHSCORE_H_
    #define HIGHSCORE_H_
    
    //Avoid global variables
    //extern int highscore[];
    //extern int test[];
    
    int SaveHighscore(int* test, int size);
    void LoadHighscore(int* highscore, int size);
    
    #endif /*HIGHSCORE_H_*/
    Highscore.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include "highscore.h"
    
    // open file
    int SaveHighscore(int* test, int size)
    {
    	FILE* fil = fopen("highscore.txt", "w");
    	if (fil == NULL)
    	{
    		printf("Unable to open file\n");
    		return 0;
    	}
    	//fwrite(test, sizeof(test[0]), 11, fil);
    	if (fwrite(test, sizeof(test[0]), size, fil) != 1)
    	{
    		printf("Unable to write to file\n");
    		return 0;
    	}
    	fclose(fil);
    	return 1;
    }
    
    void LoadHighscore(int* highscore, int size)
    {
    	FILE* stream = fopen("highscore.txt", "r");
    	if(stream == NULL)
    	{
    		printf("Error: can't open file.\n");
    		return;
    	}
    	else
    	{
    		printf("File opened.\n");
    	}
    
    	fread(highscore, sizeof(highscore[0]), size, stream);
    	for (int i = 0; i < 11; i++)
    	{
    		printf("File read: %d \n", highscore[i]);
    	}
    	fclose(stream);
    }
    Main.c:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <assert.h>
    #include "SDL.h"
    #include "SDL_ttf.h"
    #include "drawline.h"
    #include "triangle.h"
    #include "teapot_data.h"
    #include "sphere_data.h"
    #include "object.h"
    #include "text.h"
    #include "list.h"
    #include "highscore.h"
    
    int highscore[11] = {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9};
    int test[11] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
    
    int main(int argc, char** argv)
    {
    	// Start game
    	for(;;)
    	{
    		SaveHighscore(test, sizeof(test) / sizeof(test[0]));
    		LoadHighscore(highscore, sizeof(highscore) / sizeof(highscore[0]));
    	}
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    Thanks for all the help, it's muchly appreciated. I finally got it to work.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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