Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Writing to file using fwrite?

  1. #11
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Writing to file using fwrite?

    Basically, you will really need a more involved save/load function than just a call to fwrite(). As was said, if you just fwrite() the contents of your struct, it will write only the addresses of the strings, not their contents. Needless to say, dumping everything together in one fwrite() is also not portable.
    「明後日の夕方には帰ってるからね。」


  2. #12
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Writing to file using fwrite?

    Quote Originally Posted by fallenshadow View Post
    I have a bit of a problem though, this code is used within GameEditor and it seems I can't use strings. :/

    This is why I am using Char*.
    The sequence of bytes your "char *" points to is colloquially known as "string".

  3. #13
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    810
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Writing to file using fwrite?

    So, ye mean something like printing line by line for the Char* elements?

    Code:
    File *stream;
    
    char *s = "this is a string";
    char c = '\n';
    
        stream = fopen("results","w");
        
        fprintf(stream,"%s%c",s,c);
    //etc.
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  4. #14
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,273
    Distro
    Kubuntu

    Re: Writing to file using fwrite?

    Quote Originally Posted by fallenshadow View Post
    So, ye mean something like printing line by line for the Char* elements?

    Code:
    File *stream;
    
    char *s = "this is a string";
    char c = '\n';
    
        stream = fopen("results","w");
        
        fprintf(stream,"%s%c",s,c);
    //etc.
    Get into the habit of specifying pointers to hard-code string literals as const char*. As for the fprintf(), it works very similar to printf().
    Code:
    const char* s = "this is a string";
    
    FILE* stream = fopen("whatever", "w");
    
    if (stream)
    {
        fprintf(stream, "%s\n", s);      // note that I included the newline in the formatting.
    }
    else
    {
        fprintf(stderr, "Failed to open file.\n");
    }

  5. #15
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Writing to file using fwrite?

    Quote Originally Posted by fallenshadow View Post
    So, ye mean something like printing line by line for the Char* elements?

    Code:
    File *stream;
    
    char *s = "this is a string";
    char c = '\n';
    
        stream = fopen("results","w");
        
        fprintf(stream,"%s%c",s,c);
    //etc.
    For all elements... Unless you are saving megabytes of data and/or saving/loading them very frequently, save them to a text file. To read them you just need a bunch of fscanf()'s that are the direct symmetric of the fprintf()'s.
    Last edited by ofnuts; September 18th, 2013 at 08:49 AM.

  6. #16
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    810
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Writing to file using fwrite?

    Thanks everyone! Whoa this thread got long fast!

    I know people probably *facepalm* when they see my posts but I do appreciate all the advice. Im gonna have a go at this over the weekend and report back on the result.
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  7. #17
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    810
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Writing to file using fwrite?

    So I got the file writing going which was easy. The reading is a bit more involved.

    Using fscanf I got the return value which is 1. Therefore its finding my value. The question is, how does one get at the actual value?
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

  8. #18
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,294
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Writing to file using fwrite?

    fscanf() works exactly like scanf(). You give a pointer of the appropriate type and with sufficient space, and the read data gets put where the pointer says.
    「明後日の夕方には帰ってるからね。」


  9. #19
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,273
    Distro
    Kubuntu

    Re: Writing to file using fwrite?

    Quote Originally Posted by fallenshadow View Post
    So I got the file writing going which was easy. The reading is a bit more involved.

    Using fscanf I got the return value which is 1. Therefore its finding my value. The question is, how does one get at the actual value?
    The return value from fscanf() will indicate how many patterns in your format criteria were satisfied. If you are getting a value of 1, then this implies that one format specifier was matched.

    Can you elaborate on your question "... how does one get at the actual value?". My crystal ball has been furloughed and hence is not functioning.

    P.S. Based on your original posts in this thread, I conjured the following simple program to demonstrate the writing of data using fprintf() and the reading of such using fscanf(). Perhaps this may help you.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    
    
    struct MyData
    {
    	int  value1, value2;
    	char str[80];
    };
    
    
    void writeData(struct MyData* data, FILE* fp);
    void readData(struct MyData* data, FILE* fp);
    
    
    int main()
    {
    	struct MyData w_data;
    	struct MyData r_data;
    
    	w_data.value1 = 10;
    	w_data.value2 = 20;
    	strncpy(w_data.str, "This is a good day", sizeof(w_data.str));
    
    	FILE* fp = fopen("MyData.txt", "w+");
    
    	if (fp)
    	{
    		writeData(&w_data, fp);
    		fclose(fp);
    	}
    
    	fp = fopen("MyData.txt", "r");
    
    	if (fp)
    	{
    		readData(&r_data, fp);
    		fclose(fp);
    	}
    
    	assert(w_data.value1 == r_data.value1);
    	assert(w_data.value2 == r_data.value2);
    	assert(strcmp(w_data.str, r_data.str) == 0);
    
    	return 0;
    }
    
    
    void writeData(struct MyData* data, FILE* fp)
    {
    	assert(data != NULL);
    	assert(fp != NULL);
    
    	fprintf(fp, "%d\n", data->value1);
    	fprintf(fp, "%d\n", data->value2);
    	fprintf(fp, "%s\n", data->str);
    }
    
    
    void readData(struct MyData* data, FILE* fp)
    {
    	assert(data != NULL);
    	assert(fp != NULL);
    
    	fscanf(fp, "%d [^\n]", &data->value1);
    	fscanf(fp, "%d [^\n]", &data->value2);
    	fscanf(fp, "%79[^\t\n]", data->str);
    }

  10. #20
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    810
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Writing to file using fwrite?

    Thanks dwhitney, this code is excellent for the structs. However, I was having a problem with a simple int. I can store the value but not read it. *blushes*

    Code:
     FILE * READ = fopen(fileName, "r");
     
     while (!feof(READ))
     {
       gold = fscanf(READ, "%d [^\n]", &gold);
     }
     fclose(READ);
    It is returning 1 but the actual value in the text file is 4.
    Projects: PhotoFiltre LX | Xwii | URT-2D | TTM

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •