[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

error on reading data from file to sturct

Posted on 1998-10-07
9
Medium Priority
?
299 Views
Last Modified: 2010-04-15
/*

     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
Comment
Question by:dissathep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 210 total points
ID: 1253415
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
 

Author Comment

by:dissathep
ID: 1253416
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
 

Author Comment

by:dissathep
ID: 1253417
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1253418
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
 

Author Comment

by:dissathep
ID: 1253419
The suggestion not work.It has the same result.Do you have any suggestion?.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253420
If you post the source code that includes my suggestion I could try to see what'ss wrong.
0
 

Author Comment

by:dissathep
ID: 1253421
/*

     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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253422
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
 
LVL 16

Expert Comment

by:imladris
ID: 1253423
\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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question