Thread: How to read files into a struct

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Exclamation How to read files into a struct

    Is it possible to read dat files into a struct and if so how the heck do we do that??? Oh and then I need to take the struct and turn it into a node.....

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://groups.google.com/groups/sear...+c&qt_s=Search
    http://groups.google.com/groups/sear...ct&qt_s=Search

    Do or do not, there is no try. But um. In your case, you should really try, because yr whiny.

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Maybe, but at least I'm self aware.... That helped a little....I'm working in C trying to read a dat. file into a struct....if I wasn't clear before.

    Tonto...your a sad and lonely person aren't you??? If you ever need to talk about all that anger....I suppose I could make some time.......
    Last edited by verbity; 11-23-2006 at 11:31 PM.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Maybe, but at least I'm self aware

    Zing! Where'd you come up with that one? Ego tripping? LSD tripping? Brushing your teeth? Have an identity crisis and lie to yourself to to rationalize the fact that you're wrong?

    >> I'm working in C trying to read a dat. file into a struct....if I wasn't clear before.

    I posted links demonstrating techniques to read a structure from a file, and you want to read a file into a structure. Are you saying that I didn't get that? Those are exactly the same things! It doesn't make much sense to read one a file into a structure and then make an isolated node with it, but I suppose if there was a chain of such files it could make sense.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well post what you've tried, and what you've managed to determine "isn't working".

    Assuming you got the structure right, and the reading right, there is no reason why it wouldn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    I haven't tried anything yet. I've just never encountered it. I don't necessarily need an answer as much as I just need to see some prototypes or even just pseudo-code.

    Basically I have a dat file with lines like this: XXXX XXXXX XXXXXX XXXXXX
    I need to shove that data in my struct....I could just read it in, what I do not know is how to split it up. The only thing I've done is stuff like this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define SIZE 63
    #define LBUFSIZE 64
    
    int getWord(FILE *fp, char wbuf[], int c);
    void output(FILE *csis, char lbuf[]);
    FILE *fp;
    FILE *csis; 
    	
    
    void main(void)
    {
    	csis = fopen("csis.dat", "w");
    	fp = fopen("getty.dat", "r");
    
    	int c = 0;
    
    	char wbuf[SIZE];
    	char lbuf[LBUFSIZE];
    
    
    
    	
    		lbuf[0] = '\0';
    
    		c = getWord(fp, wbuf, c);
    
    		while(c != EOF)
    		{
    			//add 1 for space
    			if((strlen(lbuf) + 1 + strlen(wbuf)) > 62)
    			{
    				output(lbuf);
    				strcpy(lbuf, wbuf);
    			}
    			else
    			{
    				//enough space for this word
    				strcat(lbuf, " ");
    				strcat(lbuf, wbuf);
    			}
    		c = getWord(fp, wbuf, c);
    
    		}
    		if (c == EOF && strlen(lbuf) > 0)
    		{
    			output(csis, lbuf);
    		}
    		
    
    	fclose(fp);
    
    	fclose(csis);
    	
    
    }
    
    
    
    int getWord(FILE *fp, char wbuf[], int c)
    {
    	
    	
    	int i = 0;
    
    	if(isalpha(c))
    	{
    		wbuf[i] = c;
    		i++;
    	}
    
    	c = fgetc(fp);
    
    	while(c !=EOF && isalpha(c))
    	{
    	
    		wbuf[i] = c;
    		i++;
    		c = fgetc(fp);
    		
    	}
    	
    	wbuf[i] = '\0';	
    
    	while(c != EOF && !isalpha(c))
    	{
    		c = fgetc(fp);
    	}
    	return c;
    
    }
    void output(FILE *csis, char lbuf[])
    {
    	
    	
    
    	printf("%s\n", lbuf);
    	fprintf(csis, "%s\n", lbuf);
    
    
    }

    Which just reads in a file and then writes it out again.....so that's my sad limited knowledge...

  7. #7
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Wink

    oh and here's what my struct looks likeprobably got that wrong too)

    Code:
    typedef struct employee
    {
    	char fname[10];
    	char lname[15];
    	char gender;
    	int tenure;
    	char rate;
    	float salary;
    } *PEOPLE
    pretty simple

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, so you want to get a line using fgets(). After that, you want to seperate it using sscanf.

    And your structure looks fine. You just have to get the fgets and sscanf stuff going.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Not void main (void), int main (void).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    That void main(void) was for something else......

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What for?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Logical Aesthetician codepoet's Avatar
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by manutd
    Not void main (void), int main (void).
    seriously man, get over it.

    ignoring the question he asked and criticizing something as trivial as his coding style doesn't accomplish anything except make you look condescending, arrogant, and generally unhelpful.
    Code:
    $question =~ /(bb|[^b]{2})/;

  13. #13
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    I meant it was for that program example I put above....not the one I'm working on right now....

    I have a simple fgetc(fp) function I use to pull word for word from a file which I couple with an output() function....I think I'll just use that and modify the output function to load the struct and then build a function to load the struct into the node....why I have to put the struct into a node I don't understand....but the assignment is load the struct into a node for a file full of employees and employee info...then run linear linked list operations on it....I think this will work....but for whatever I reason I couldn't wrap my head around the problem at first.....thanks for the input !!!!
    Last edited by verbity; 11-23-2006 at 11:29 PM.

  14. #14
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Quote Originally Posted by codepoet
    seriously man, get over it.

    ignoring the question he asked and criticizing something as trivial as his coding style doesn't accomplish anything except make you look condescending, arrogant, and generally unhelpful.
    It's not a "style". The standard clearly says that it should be int main. While this is not directly related to his question, it is in his code, and to be helpful, I corrected him on it. IMO, correcting people's code is not "condescending", it's useful. That's how you learn. If you need clarification, see the FAQ.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  15. #15
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    thanks....I realize that int should be used for main....it just wasn't pertenant so I didn't throw it int here.....but I will from now on....thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 5
    Last Post: 02-01-2003, 10:58 AM
  5. The problem read and write in files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 12:54 PM