Read a file back in (C)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Read a file back in (C)

  1. #1
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    18

    Question Read a file back in (C)

    Hi,

    I've just searched for a function to read a file backin i've worked something out like this

    void leesIn(Lijst lijst)
    {
    char buffer[100];
    FILE *fptr;
    fptr=fopen("backup.txt","r");
    while(fscanf(fptr,"%s",buffer) != EOF)
    printf("%s",buffer);
    fclose(fptr);
    }

    But in my file I got this:

    Entrytype: 1
    Name: dfd
    Course: dfd
    Datum: Mon Dec 12 12:00:00 2011

    Status: 1
    Type: 0
    Duur: 9
    Lokaal: c108

    It was a struct so EntryType is a enum and that has the value of 1, but how can I separate and read everything for the : and then everything after the : , so I can check what type it is and then create again the struct and give the value of the second part after : ?

    Kind regards,

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Read a file back in (C)

    I would suggest to separately read the field type (the part before the ':') and the field contents after the ':'. I modified your function to demonstrate this:

    Code:
    void leesIn(/* Lijst lijst */)
    {
      char fieldtype[100], contents[100];
      FILE *fptr;
      fptr=fopen("backup.txt","r");
      assert(fptr);  // Make sure the file was actually opened successfully
      while(fscanf(fptr, "%[^:]: %[^\n]\n", fieldtype, contents) != EOF)
        printf("Field type: \"%s\", contents: \"%s\"\n", fieldtype, contents);
      fclose(fptr);
    }
    This uses a more complex fscanf() format string which contains format specifiers that are not really common. For documentation, see http://msdn.microsoft.com/en-us/library/xdb9w69d.aspx for instance. The format string means: Read everything up to the ':' into the first string (fieldtype), skip the ':' and the following blank, and then read everything up to the newline character into the second string (contents). I hope I didn't accidentally anything MS specific in there (or you happen to use an MS compiler).

    After that you need to decide about further processing based on the fieldtype. Unfortunately, your fields don't contain all ints. (In that case you could simply replace the "%[^\n]" in the format string with "%d" and read the value directly into an int variable.) The way it is, you'll have to convert the field contents yourself based on its type. You can convert the ints using atoi() and process strings just as-is, but I'm a bit concerned about the date. In the format you have in your file it is a bit complicated to parse and convert (unless I simply forgot about a ready-made C library function that does that for you). You could simplify that a bit by writing the date to the file in a simpler format in the first place.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    18

    Re: Read a file back in (C)

    Thanks for you help already. But we need to use time.h so it's a lib of C, that's why the time is in that format.

    I'm going to try how you explained. But how do you see it with the time then?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Read a file back in (C)

    Unfortunately, <time.h> doesn't contain any function that is able to convert a time string to a time_t. Therefore I don's see an easier way of converting the time than to parse the time string yourself out of the contents variable by using sscanf() with a format string like "%*s %s %d %d:%d:%d %d", set up a struct tm with the individual values and convert that using mktime(). (I think you can read the integer components of the time directly into the fields of the struct tm but you need to convert the string representation of the month "manually".)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    18

    Re: Read a file back in (C)

    Yeah I get it :-) but I'm sitting with a problem never did this before. But It gives me some errors to read the txt file back in the struct this is my code:

    http://pastebin.com/eGn6igCP

    This are the structs:

    http://pastebin.com/zbgFg8qC

    I don't see the problem...

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Read a file back in (C)

    Well, ok, but could you please tell me what the errors you get are and where (i.e. on which lines in case they are compiler errors) you get them? This would signifcantly simplify my life (and that of others who want to help you). I suppose it doesn't have something to do with a date because I don't see you attempting to read in one anywhere in the code.

    Your code on pastebin looks really good with all the syntax highlighting and stuff, but I think it will be more convenient for anyone involved (including yourself) to simply include it in your posts, of course using code tags. For the convenience of others and for the purpose of demonstration, here's your code within code tags:

    Code:
    #ifndef _ITEM_H
    #define _ITEM_H
    
    #include <time.h>
    
    typedef struct Subtaken
    {
    	char* subtaak;
    	struct Subtaken* volgende;
    }Subtaken;
    
    typedef struct Person
    {
    	char* person;
    	struct Person* volgende;
    }Person;
    
    typedef struct Task
    {
    	enum {urgent,high,medium,low} prioriteit;
    	Person* personen;
    	Subtaken* subtaken;
    }Task;
    
    typedef struct Exam
    {
    	enum{mondeling,schriftelijk,open,gesloten}type;
    	int duur;
    	char* lokaal;
    }Exam;
    
    typedef struct Lecture
    {
    	char* spreker;
    	char* onderwerp;
    	int duur;
    	char* lokaal;
    }Lecture;
    
    typedef struct Status
    {
    	enum{DONE,TODO}status;
    }Status;
    
    typedef struct Item
    {
    	enum {TASK, EXAM, LECTURE} entryType;
    	char* name;
    	char* course;
    	time_t date;
    	Status status;
    	union {
    		Task *task;
    		Exam *exam;
    		Lecture *lecture;
    	};
    } Item;
    
    #endif
    Code:
    void leesIn()
    {
    	Item afspraak;
    	char veldtype[100], waarde[100];
    	FILE *fptr;
    	fptr=fopen("backup.txt","r");
    	assert(fptr);  // kijken of de file succesvol kan worden geopend. 
    	while(fscanf(fptr, "%[^:]: %[^\n]\n", veldtype, waarde) != EOF)
    	{
    		if(strcmp(veldtype,"Entrytype")==0)
    		{
    			afspraak.entryType = waarde;
    		}
    		if(strcmp(veldtype,"Name")==0)
    		{
    			afspraak.name = (char *) malloc(CHARS);
    			afspraak.name = waarde;
    		}
    		if(strcmp(veldtype,"Course")==0)
    		{
    			afspraak.course = (char *) malloc(CHARS);
    			afspraak.course = waarde;
    		}
    		if(strcmp(veldtype,"Status")==0)
    		{
    			afspraak.status.status = waarde;
    		}
    		if(strcmp(veldtype,"Prioriteit")==0)
    		{
    			afspraak.task = (Task*)malloc(sizeof(Task));
    			afspraak.task->prioriteit = waarde;
    		}
    
    		if(strcmp(veldtype,"Personen")==0)
    		{
    			afspraak.task->personen = (Person*)malloc(sizeof(Person));
    			afspraak.task->personen->person = waarde;
    		}
    		if(strcmp(veldtype,"Subtaken")==0)
    		{
    			afspraak.task->subtaken = (Subtaken*)malloc(sizeof(Subtaken));
    			afspraak.task->subtaken->subtaak = waarde;
    		}
    		if(strcmp(veldtype,"Type")==0)
    		{
    			afspraak.exam = (Exam*)malloc(sizeof(Exam));
    			afspraak.exam->type = waarde;
    		}
    		if(strcmp(veldtype,"Duur")==0)
    		{
    			afspraak.exam->duur = atoi(waarde);
    		}
    		if(strcmp(veldtype,"Lokaal")==0)
    		{
    			afspraak.exam->lokaal = waarde;
    		}
    		if(strcmp(veldtype,"Spreker")==0)
    		{
    			afspraak.lecture->spreker = (char*) malloc(CHARS);
    			afspraak.lecture->spreker = waarde;
    		}
    		if(strcmp(veldtype,"Duur2")==0)
    		{
    			afspraak.lecture->duur = atoi(waarde);
    		}
    		if(strcmp(veldtype,"Lokaal2")==0)
    		{
    			afspraak.lecture->lokaal = (char*) malloc(CHARS);
    			afspraak.lecture->lokaal = waarde;
    		}
    		//printf("Veldtype: \"%s\", Waarde: \"%s\"\n", veldtype, waarde);
    	}
    	fclose(fptr);
    }
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center