Results 1 to 7 of 7

Thread: Writing from text to struct

  1. #1
    Join Date
    Oct 2004
    Posts
    393
    Rep Power
    0

    Default Writing from text to struct

    Well, i manage to write, somehow, to file using a struct (not as simple as i thought it would be). Now, am trying to read from the text then insert it to a srtuct. Using previous thread example

    typedef struct student
    {//for name
    char fname[20];
    char lname[20];

    int age;
    char id[20];
    int grade[20];
    }STUDENT;
    the text file looks like this

    andrew //firstname
    magnon //lastname
    12 //age
    124OY //id
    90 //grade
    how can i write this to memory in a structure form?

    please provide a code for me to follow
    Moving forward never back. That's my way of life.

  2. #2
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    you cant and you don't need to if you just want to read back the contents into a struct.

  3. #3
    Join Date
    Oct 2004
    Posts
    393
    Rep Power
    0

    Default

    y not? if u can write from a struct?

    #include<stdio.h>
    #include<stdlib.h> //to do the exiting

    //function to save
    void save(STUDENT info){
    FILE *savedfile; //pointer

    savedfile=fopen("c:/database.txt","w"); //creates a file on drive "c" named "database.txt" to be written to

    if(savedfile == NULL) //testing if file is opened
    {printf(" Error opening output file");
    exit(0);} //from the library stdlib.h. will exit if file is already opened

    fprintf(savedfile,"%s, %s, %d, %s, %d", info.fname,info.lname,info.age,info.id,info.grade) ;//writes struct info to file

    fclose(savedfile);//closes file after writing
    }
    y cant something similar be done to read from text to struct?
    besides, if wanted to edit the data, would i not need to do save back to memeory in a struct format?
    Moving forward never back. That's my way of life.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Data was loss

    Before I explain, there are two ways you can approach this.

    The 1st way is to serailize the struct as you have done to a binary stream. You must make sure the the write was done in binary without any loss of information. Then you can deserialize it from binary stream. When viewing the binary file, the information may have regions that are unreadable, plus not every text programs allows viewing of binary files. This method basically dumps the memory associted with the struct to the hard drive. Try to open the file in notepad or the binary editor in VS to see the contents. A precation should be taken to make sure that the struct is packed the same way for writting and reading, using #pragma pack.

    The second way is carefully save each part of the struct. This I think was what the home work was about. You can save it to a compatable text file. This means no odd characters are present oout of the text character set. This method also breaks down each attribute of the struct. The unreadable binary information in the struct package is left out and only the text data is written you can even format the text as you want, doing this for each attribute, eg like putting an attribute in each new line. The text file would be easily viewable and understood, and to read the text file just reverse the process.

    It depends on which method you want. BTW, binary saving does not provide encryption but it has performance for fast loading and saving. Binary is significantly more difficult to manipulate than text and you are likely to mess it up and have to do a lot of testing. It may be a better trade off to work with text save.

    Summary
    You would find a binary file hard to work with to maintain a database, but a text file which separated the info by new lines would be easier.
    Last edited by crosswire; Mar 26, 2006 at 08:48 PM.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Oct 2004
    Posts
    393
    Rep Power
    0

    Default

    thanks for all the info..

    Crosswire, could u give an example of the second method?

    and is it possible to read a line of text that contains spaces.. ive been tryin and everything have proven futile so far.
    Moving forward never back. That's my way of life.

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    //
    Code:
    #include<stdio.h>
    #include<stdlib.h> //to do the exiting
    
    typedef struct student
    {//for name
    char fname[20];
    char lname[20];
    
    int age;
    char id[20];
    int grade; //Correction
    }STUDENT;
    
    //function to save
    void load(STUDENT & info){
    	FILE *loadedfile; //pointer
    
    	loadedfile=fopen("c:/database.txt","r"); //creates a file on drive "c" named "database.txt" to be written to
    
    	if(loadedfile == NULL) //testing if file is opened
    	{printf(" Error opening output file");
    	exit(0);} //from the library stdlib.h. will exit if file is already opened
    
    	fscanf(loadedfile,"%s", &(info.fname)) ;//writes struct info to file
    	fscanf(loadedfile,"%s", &(info.lname)) ;//writes struct info to file
    	fscanf(loadedfile,"%d", &(info.age)) ;//writes struct info to file
    	fscanf(loadedfile,"%s", &(info.id)) ;//writes struct info to file
    	fscanf(loadedfile,"%d", &(info.grade)) ;//writes struct info to file
    
    	fclose(loadedfile);//closes file after writing
    } 
    
    void main()
    {
    	student	j;
    	load(j);   //Place a break point here. Put j in the Debug Watch Window then click Step Over
    }//
    fscanf does not seem to work the scan as fprintf with the format string.

    I tested once still
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Oct 2004
    Posts
    393
    Rep Power
    0

    Default

    Nuff respect yow. The fscanf doesnt provide much flexibility. I remanipulated the data to suit the criteria none-the-less.
    Moving forward never back. That's my way of life.

Posting Permissions

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