[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

error on reading data from file to sturct

/*

     PROGRAM SORT VERSION BETA

*/
#include<stdio.h>
#include<stdlib.h>
#include<io.h>
#include<string.h>
#include<fcntl.h>


#include<conio.h>


#define DATASIZE 54
#define MARK 8
#define SPACE 8
#define HVALUE 7
#define DATE 14
#define SPACE2 4
#define TYPE 3
#define CR_LF 2

int main(int argc,char *argv[])
{
    FILE *fr,*fw;

    struct R_data{                         //struct of raw data
            struct R_data *before;
            char mark[MARK];
            char space1[SPACE];
            char H_value[HVALUE];
            char space2[SPACE];
            char date[DATE];
            char space3[SPACE2];
            char type[TYPE];
            char cr_lf[CR_LF];
            struct R_data *next;
            };

    struct R_data *Head,*Point,*Tail,*Phead,*Ptail;

/*------------------------------------------------------------------------*/

    if((fr = fopen(argv[argc-1],"r+t")) == NULL)
    {
       fprintf(stderr,"Cannot open input file.\n");
       return(1);
    }
    else
    {
       //initialize header of struct
       Head  = (struct R_data *)malloc(sizeof(struct R_data));
       Phead = Head;
       Head->before = NULL;
       Head->next   = NULL;
       while(feof(fr) == 0)
       {
        Point = (struct R_data *)malloc(sizeof(struct R_data));
        Phead->next = Point;
        Point->before = Phead;
        Phead = Phead->next;
        Phead->next = NULL;
        fread(Phead->mark,MARK,1,fr);
        fread(Phead->space1,SPACE,1,fr);
        fread(Phead->H_value,HVALUE,1,fr);
        fread(Phead->space2,SPACE,1,fr);
        fread(Phead->date,DATE,1,fr);
        fread(Phead->space3,SPACE2,1,fr);
        fread(Phead->type,TYPE,1,fr);
        fread(Phead->cr_lf,CR_LF,1,fr);

       }
       Point = (struct R_data *)malloc(sizeof(struct R_data));
       Phead->next = Point;
       Point->before = Phead;
       Point->next = NULL;
       Tail = Point;
       /*test*/

       Point = Head->next;
       while(Point != Tail)
       {
        printf("\n %s",Point->date);
        Point = Point->next;
        getch();
       }
       Point = Head;
       Phead = Head->next;
       while(Point <= Tail)
       {
       free(Point);
       Point = Phead;
       Phead = Phead->next;
       }

       /*test*/
       fclose(fr);
       return(0);
    }
}


******************************************************************************************************************************
Data characteristic
########        + 0.123        19980929145856    BSC
########        + 0.123        19980929145841    BSC
########        + 0.123        19980929145836    BSC


 Above is my program , use to read data from file that have characteris above and fill to struct for make link list.But problem is when I print the test result by " printf("\n %s",Point->date);  "  command.The result should be 1998... but the appearance is "19980929145856    BSC  " and follow by the character that can't read.
  Where the wrong in my program and how can I correct ?.
0
dissathep
Asked:
dissathep
  • 4
  • 4
1 Solution
 
scrapdogCommented:
Your problem is that you are printing a string, and a NULL must be present to terminate the string.

When you read in the date, you are reading 14 characters from the file.  The array size is 14.  Since the last character in the array is 14, you array is full.  There's no room for the NULL.  Printf keeps printing all contiguous bytes until a NULL is found, it doesn't care what the size of your array is.

My suggestion is to define the length of each of the fields you are reading to one greater.

For example:

   char date[DATE];

should be changed to

   char date[DATE+1];

and so on.

This 15th character will leave room for the NULL when it comes time to print it out.  However, you will have to insert the NULL manually after you read the string from disk.

Like this:

fread(Phead->date,DATE,1,fr);
Phead->date[DATE] = NULL;

This will insert the NULL right into the data structure between the date and the space.  When you printf the date, all you will see is the fourteen characters of the date.

The same would go for any members of your data structure that you would want to print as a string.


---------------------------------------------------------
Of course, if you didn't want to do it this way (change the data structure), you could print the characters one at a time:

for(i=0;i<=(DATE-1);i++) printf("%c",date[i]);



scrapdog
0
 
dissathepAuthor Commented:
After read data from file to fill in sturct ,I will sort this data by date and write it back to a new file .Following write back finish I 'll take a new file to calculate again.So I don't want to change data from the original.Do you have any suggestion to correct correct my program?.
                                                                                  Thank a lot.
0
 
dissathepAuthor Commented:
After read data from file to fill in sturct ,I will sort this data by date and write it back to a new file .Following write back finish I 'll take a new file to calculate again.So I don't want to change data from the original.Do you have any suggestion to correct correct my program?.
                                                                                  Thank a lot.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
scrapdogCommented:
Even if you change your struct, you won't necessarily be changing the data.

Be inserting a NULL between the data and the space, it will be much easier to sort, because you can handle the date as a string, and pass it to strcmp, or whatever you will use.

When you write the file back to disk, you don't have to write the extra character.  All you have to do is fwrite it back the exact way you fread it.  Then the null won't be included in the file.

Example, if you fread it like this:

fread(Phead->date,DATE,1,fr);

then fwrite it like this:

fwrite(Phead->date,DATE,1,fw);  {or whatever you use for a stream}

This will write to disk exactly as you found it.  The extra array element for NULL would only exist in *memory*, for the purpose of manipulating it as a string.


On disk before fread:
............       (14 chars)
19980929145856

In memory after fread and insert a NULL
.............
19980929145856N    (where N is NULL)

You will write back only 14 chars of this (what was read), so

On disk after fwrite
............
19980929145856


The ONLY change that you have to make is

char date[DATE+1]

and then insert the null in date[DATE]

As long as you fwrite the same amount you fread, nothing will change.
0
 
dissathepAuthor Commented:
The suggestion not work.It has the same result.Do you have any suggestion?.
0
 
scrapdogCommented:
If you post the source code that includes my suggestion I could try to see what'ss wrong.
0
 
dissathepAuthor Commented:
/*

     PROGRAM SORT VERSION BETA

*/
#include<stdio.h>
#include<stdlib.h>
#include<io.h>
#include<string.h>
#include<fcntl.h>


#include<conio.h>


#define DATASIZE 54
#define MARK 8
#define SPACE 8
#define HVALUE 7
#define DATE 14
#define SPACE2 4
#define TYPE 3
#define CR_LF 2

int main(int argc,char *argv[])
{
    FILE *fr,*fw;

    struct R_data{                         //struct of raw data
            struct R_data *before;
            char mark[MARK+1];
            char space1[SPACE+1];
            char H_value[HVALUE+1];
            char space2[SPACE+1];
            char date[DATE+1];
            char space3[SPACE2+1];
            char type[TYPE+1];
            char cr_lf[CR_LF+1];
            struct R_data *next;
            };

    struct R_data *Head,*Point,*Tail,*Phead,*Ptail;

/*------------------------------------------------------------------------*/

    if((fr = fopen(argv[argc-1],"r+t")) == NULL)
    {
       fprintf(stderr,"Cannot open input file.\n");
       return(1);
    }
    else
    {
       //initialize header of struct
       Head  = (struct R_data *)malloc(sizeof(struct R_data));
       Phead = Head;
       Head->before = NULL;
       Head->next   = NULL;
       while(feof(fr) == 0)
       {
        Point = (struct R_data *)malloc(sizeof(struct R_data));
        Phead->next = Point;
        Point->before = Phead;
        Phead = Phead->next;
        Phead->next = NULL;

        Phead->mark[MARK] = NULL;
        Phead->space1[SPACE] = NULL;
        Phead->H_value[HVALUE] = NULL;
        Phead->space2[SPACE] = NULL;
        Phead->date[DATE] = NULL;
        Phead->space3[SPACE2] = NULL;
        Phead->type[TYPE] = NULL;
        Phead->cr_lf[CR_LF] = NULL;

        fread(Phead->mark,MARK,1,fr);
        fread(Phead->space1,SPACE,1,fr);
        fread(Phead->H_value,HVALUE,1,fr);
        fread(Phead->space2,SPACE,1,fr);
        fread(Phead->date,DATE,1,fr);
        fread(Phead->space3,SPACE2,1,fr);
        fread(Phead->type,TYPE,1,fr);
        fread(Phead->cr_lf,CR_LF,1,fr);

        

       }
       Point = (struct R_data *)malloc(sizeof(struct R_data));
       Phead->next = Point;
       Point->before = Phead;
       Point->next = NULL;
       Tail = Point;
       /*test*/

       Point = Head->next;
       while(Point != Tail)
       {
        int i;
        printf("%s\n",Point->date);
        Point = Point->next;
        getch();
       }
       Point = Head;
       Phead = Head->next;
       while(Point <= Tail)
       {
       free(Point);
       Point = Phead;
       Phead = Phead->next;
       }

       /*test*/
       fclose(fr);
       return(0);
    }
}


This 's the source after correct.Pls tell me the code of Carriage-Return and End-of-File used within"strcmp()"command . I 'm not sure that use "\r" ,"\n","\0".
0
 
scrapdogCommented:
Try setting the nulls AFTER you read the file.

Like this

fread(Phead->mark,MARK,1,fr);
  fread(Phead->space1,SPACE,1,fr);
  fread(Phead->H_value,HVALUE,1,fr);
  fread(Phead->space2,SPACE,1,fr);
  fread(Phead->date,DATE,1,fr);
  fread(Phead->space3,SPACE2,1,fr);
  fread(Phead->type,TYPE,1,fr);
  fread(Phead->cr_lf,CR_LF,1,fr);
  Phead->mark[MARK] = NULL;
  Phead->space1[SPACE] = NULL;
  Phead->H_value[HVALUE] = NULL;
  Phead->space2[SPACE] = NULL;
  Phead->date[DATE] = NULL;
  Phead->space3[SPACE2] = NULL;
  Phead->type[TYPE] = NULL;
  Phead->cr_lf[CR_LF] = NULL;

See if this works.

\0 tells strcmp that the end of the string has been reached.

\n and \r are the same thing (i think).  Both of these represent ASCII #13, the carriage return.  

(I am not sure but) I don't think \n and \r affect strcmp.  \0 (NULL) does though.


0
 
imladrisCommented:
\n an \r do not affect strcmp, however they are not the same. \n represents a newline or linefeed and is encoded 0x0a. \r is a carriage return: 0x0d.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now