Input output with structures

This is a discussion on Input output with structures within the C Programming forums, part of the General Programming Boards category; Hello everybody! This is new area for me. Structures and File I\O. I need to create structure date that will ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    Input output with structures

    Hello everybody!

    This is new area for me. Structures and File I\O.
    I need to create structure date that will contain the following elements:

    Code:
    struct date {
       int month;
       int day;
       int year;
    }reordDate;
    
    struct date blankDate = {0,0,0};
    This is the date that is initialized to zero. This structure is nested into the one that is bigger and most crucial here:

    Code:
    struct invent{
    	char partId[6+1];
    	char partDesc[25+1];
    	double unitPrice;
    	int quanOnHand;
    	int reordPoint;
    	int reordQuant;
    	struct reordDate;
    	int delFlag;
    }item;
    Now, after I have this created I need to copy test data that will be created in text file.

    Test data:

    Part ID Description Unit Price QOH ROP ROQ

    Data goes here.

    C program will read this data and populate a struct that meets specifications in invent structure.

    This is the part I don't understand:

    Code:
    char ch, lastch = ' ';
    char tempLine[40];
    char bufferLine[80];
    
    typedef struct{
               char partid [6+1];
               long recoffst;
    }INDEX;
    INDEX indxpt [100];
    Why is lastch is declared with the ' ' space character. I understand that tempLine and bufferLine are arrays where read data will be temporary stored. typedef struct with only two elements and partid that is repeated from the first structure. What is the purpose of this structure? Variable for typedef structure is index, and now I have array of structures that is equal to 100 of these. I don't understand typedef structures, is that different than when you declare struct plus tag and elements and variable. I tried to find this answer in the book but it is very confusing.

    These structures were declared before main function so that means it is global for whole program. I understand that.

    I need to write the structure to a binary file opened in w+ mode:

    Code:
    main()
    {
       if((inPut = fopen("part.txt","r"))!= NULL)
         if((outPut = fopen("invent.dat","wb+"))!= NULL)
           if (fgets(bufferLine, 80, inPut)==NULL)
    	goodrd = 0;
    	   else
    	     puts(bufferLine);
    }
    Here my indentation could be bad, but I don't know which of these are nested.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    typedef just allows you to rename something to make it more easy to reference, ie:

    typedef unsigned int UINT;

    now you can do "UINT blah;" and it will be as though you did unsigned int blah;

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > char ch, lastch = ' ';
    I've no idea either - they seem surplus to the problem as far as I can tell

    Your first step is to read the text file one line at a time, using fgets()
    Parse that line for information, storing relevant data in your item variable
    There are many ways of doing this, but perhaps sscanf() is the most consise
    Write the record stored in item to the output file, using fwrite.


    > What is the purpose of this structure?
    To create an index of the file you create. Basically, you search the index for a matching part number, and the offset tells you where in the file the actual complete record is. Creating it is pretty easy once you've got the first bit above up and running.

    > I don't understand typedef structures
    typedef is simply a means of attaching a shorter name to complex declarations - and some of them get really complicated.
    In your example, it only saves you one word.
    These are equivalent.
    Code:
    typedef struct {
               char partid [6+1];
               long recoffst;
    } INDEX;
    INDEX indxpt [100];
    
    struct index {
               char partid [6+1];
               long recoffst;
    };
    struct index indxpt [100];
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    I tried to execute this. My compiler doesn't like it.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "C:\Documents and Settings\10111260\My Documents\CrtUtil.h"
    FILE *inPut, *outPut;
    struct date{
    	int month, day, year;}reordDate;
    	struct date blankDate = {0,0,0};
    struct invent{
    	char partId[6+1];
    	char partDesc[25+1];
    	double unitPrice;
    	int quanOnHand;
    	int reordPoint;
    	int reordQuant;
    	struct reordDate;
    	int delFlag;
    }item;
    
    int i, j, goodrd = 1;
    int delFlag;
    
    char ch, lastch = ' ';
    char tempLine[40];
    char bufferLine[80];
    
    typedef struct{
    	char partid [6+1];
    	long recoffst;
    }INDEX;
    INDEX indxpt [100];
    
    main()
    {
    	if((inPut = fopen("C:\\Documents and Settings\\10111260\\My 
    Documents\\part.txt","r"))!= NULL)//CHECKING IF I HAVE INPUT FILE
    		if((outPut = fopen("C:\\Documents and Settings\\10111260\\My 
    Documents\\invent.dat","wb+"))!= NULL)//CHECKING IF I HAVE OUTPUT FILE
    		
    		{if (fgets(bufferLine, 80, inPut)==NULL)//IF GETS = NULL goodrd IS 
    FALSE
    			goodrd = 0;
    		else
    			puts(bufferLine);//ELSE bufferLine = THE LINE READ
    		
    		while(goodrd)//WHILE goodrd IS TRUE
    			{lastch = ' ';//lastch = NULL
    		    reordDate = blankDate;
    			delFlag = 0;//delFlag is set to 0
    		
    			for( i = 0, j = 0; lastch != ','; i++, j++)
    					{sscanf(&bufferLine[i], "%c",&ch);//reading the bufferLine 
    character by character
    					if(ch != ',')//if  ch is not equal to ',' 
    						tempLine[j] = ch;//the character is send to tempLine
    						lastch = ch;}//lastch = character read
    				for( j = 0; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				
    				for( j = 0,; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				
    			}
    		}
    
    		tempLine[j-i] = '\0';//tempLine is set to a NULL or in other words is 
    clear
    
    		strcpy(item.partId, tempLine);
    
    	fwrite (&item, sizeof(struct invent),1, outPut);
    
    	/*fseek (outPut,0L, SEEK_SET);
    	
    	fread (&item, sizeof(struct invent,1, inPut);
    		while(!feof(outPut))
    			offset = ftell(outPut);
    */
    		printf("get character\n");
    
    		getch;
    	return 0;
    }
    My compiler pointed at this:

    Code:
    struct invent{
    	char partId[6+1];
    	char partDesc[25+1];
    	double unitPrice;
    	int quanOnHand;
    	int reordPoint;
    	int reordQuant;
    	struct reordDate;
    	int delFlag;
    }item;
    It says that 'type name' has incomplete type. What is highlighted in red that is where problem is.

    Thanks.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct invent{
    	char partId[6+1];
    	char partDesc[25+1];
    	double unitPrice;
    	int quanOnHand;
    	int reordPoint;
    	int reordQuant;
    	struct reordDate variableNameGoesHere;
    	int delFlag;
    }item;
    You didn't give your variable a name. Basicly what you did is:
    Code:
    struct foo
    {
        int;
    };
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    I have another error message on line 27
    [Warning] multi-line string literals are deprecated.
    I counted the lines and it points at the line between following codes:

    Code:
    char ch, lastch = ' ';
    char tempLine[40];
    char bufferLine[80];
    Line 27 is in red. Why it pointed at that line and what this error message means.

    Thanks everybody for response.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    Hi quzah!

    I forgot to tell you how I fixed this problem. I put tag name inside and I think it works fine now. My compiler doesn't point at that spot anymore.

    This is what I did:

    Code:
    struct date reordDate;
    I hope that it is OK.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by barim
    Hi quzah!

    I forgot to tell you how I fixed this problem. I put tag name inside and I think it works fine now. My compiler doesn't point at that spot anymore.

    This is what I did:

    Code:
    struct date reordDate;
    I hope that it is OK.
    That's perfect. That's what it wanted you to do. I assumed that 'recordDate' was the actual name of the structure, and you'd left out the instance name. Apparently you had it the other way around. Same end result though.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if((inPut = fopen("C:\\Documents and Settings\\10111260\\My 
    Documents\\part.txt","r"))!= NULL)//CHECKING IF I HAVE INPUT FILE
    		if((outPut = fopen("C:\\Documents and Settings\\10111260\\My 
    Documents\\invent.dat","wb+"))!= NULL)//CHECKING IF I HAVE OUTPUT FILE
    It's actually probably talking about these two lines. See how My is one one line, and Documents is on another? That should all be on one line. Otherwise, you can make the last character in the line a \ and it will recognize that you're trying to continue that string but have basicly run out of line room. Like:
    Code:
    char foo[] = "hello \
    world";
    
    printf("%s", foo );
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    Thanks quzah!

    I have another error message. I assume it is something about the brackets. This is whole complete code again.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *inPut, *outPut;
    struct date{
    	int month;
        int day;
        int year;}reordDate;
    	struct date blankDate = {0,0,0};
    struct invent{
    	char partId[6+1];
    	char partDesc[25+1];
    	double unitPrice;
    	int quanOnHand;
    	int reordPoint;
    	int reordQuant;
    	struct date reordDate;
    	int delFlag;
    }item;
    
    int i, j, goodrd = 1;
    int delFlag;
    
    char ch, lastch = ' ';
    char tempLine[40];
    char bufferLine[80];
    
    typedef struct{
    	char partid [6+1];
    	long recoffst;
    }INDEX;
    INDEX indxpt [100];
    
    main()
    {
    	if((inPut = fopen("C:\\Documents and Settings\\My Documents\\part.txt","r"))!= NULL)
    		if((outPut = fopen("C:\\Documents and Settings\\My Documents\\invent.dat","wb+"))!= NULL)
    		
    		{if (fgets(bufferLine, 80, inPut)==NULL)
    
    			goodrd = 0;
    		else
    			puts(bufferLine);
    		
    		while(goodrd)
    			{lastch = ' ';//lastch = NULL
    		    reordDate = blankDate;
    			delFlag = 0;
    		
    			for( i = 0, j = 0; lastch != ','; i++, j++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    					if(ch != ',')
    						tempLine[j] = ch;
    						lastch = ch;}
    				for( j = 0; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    							
    				
    				for( j = 0,; lastch != ','; j++, i++)					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				for( j = 0,lastch = NULL; lastch != ','; j++, i++)
    					{sscanf(&bufferLine[i], "%c",&ch);
    						if(ch != ',')
    							tempLine[j] = ch;
    							lastch = ch;}
    				
    			}
    		}
    
    		tempLine[j-i] = '\0';
    clear
    
    		strcpy(item.partId, tempLine);
    
    	fwrite (&item, sizeof(struct invent),1, outPut);
    
    	/*fseek (outPut,0L, SEEK_SET);
    	
    	fread (&item, sizeof(struct invent,1, inPut);
    		while(!feof(outPut))
    			offset = ftell(outPut);
    */
    		printf("get character\n");
    
    		getch;
    	return 0;
    }
    Error message is:

    parse error before ';' token

    I highlighted problematic spot in red.

    Thanks.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( j = 0,; lastch != ','; j++, i++)
    Just reread the line carefully.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM
  3. Input & Output Explain?
    By 98dodgeneondohc in forum C Programming
    Replies: 5
    Last Post: 04-24-2005, 06:13 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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