text to Binary understanding

text to Binary code understanding

Page 1 of 1

3 Replies - 823 Views - Last Post: 01 October 2010 - 06:34 AM Rate Topic: -----

#1 fatbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 10

text to Binary understanding

Posted 01 October 2010 - 05:53 AM

Hi All

I have a small assignment in C. The teacher has provided us with the following code. I am unable to understand anything after "r = (struct record*) malloc(sizeof(struct record));". I am fairly new to programming. can some one please explain the code to me ? Any help would be great.

void readTextAndWriteBinary(char *fileName)
{
    FILE* infile, *outfile;
    char buff[255];
    char *pch;
    struct record* r;
    int counter = 0;
    
    infile = fopen(fileName,"r");	/* open text file for reading */
    if (infile == 0){
       perror("Error opening text file");
       return;
    }
    
    outfile=fopen("testdata.dat","wb");	/* open binary file for writing */
    
    r = (struct record*) malloc(sizeof(struct record));    
    
    /* skip the first line */
    fgets(buff, 255, infile);
    /* read text file line by line */
    while (!feof(infile)){
      fgets(buff, 255, infile);
      if (strlen(buff) <= 10)
        break;
      pch = strtok(buff, ",");
      (*r).transType = pch[0]; /* transtype */
      pch = strtok(0, ",");
      strcpy((*r).seriesID, pch); /* seriesID */
      pch = strtok(0, ",");
      if (counter % 2 == 0)/* productID */
          sprintf((*r).productID, "%d", counter);
      else
          sprintf((*r).productID, "%d", 353625 - counter);
      pch = strtok(0, ","); /* skip empty field - flag */
      pch = strtok(0, ",");
      (*r).month = atoi(pch); /* month */
      pch = strtok(0, ","); /* skip empty field - ex */
      pch = strtok(0, ",");
      (*r).version= atoi(pch); /* version */      
      pch = strtok(0, ",");
      (*r).generation = atoi(pch); /* generation */
      pch = strtok(0, ",");
      (*r).index1 = pch[0]; /* index1 */
      pch = strtok(0, ",");
      (*r).index2 = pch[0]; /* index2 */
      pch = strtok(0, ",");
      (*r).orderPrice= atof(pch); /* orderPrice */
      pch = strtok(0, ",");
      strcpy((*r).invStart, pch); /* invStart */
      pch = strtok(0, ",");
      strcpy((*r).invEnd, pch); /* invEnd */
      pch = strtok(0, ",");
      (*r).orderSize= atoi(pch); /* orderSize */
      pch = strtok(0, ",");
      (*r).numOrders= atoi(pch); /* numOrders */
      pch = strtok(0, ",");
      (*r).aggregate= atoi(pch); /* aggregate */

      /* write binary data */
      fwrite((struct record*)r,sizeof(struct record),1,outfile);
      
      counter++;
    }
    
    fclose(infile);
    fclose(outfile);
    printf("Binary file created.\n");
}   /* readTextAndWriteBinary */


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

Is This A Good Question/Topic? 0
  • +

Replies To: text to Binary understanding

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: text to Binary understanding

Posted 01 October 2010 - 05:57 AM

:code:
Thats a lot of code you want explaining. Perhaps ask a specific question, and when that's answered, ask another.

This post has been edited by simeesta: 01 October 2010 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 fatbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 10

Re: text to Binary understanding

Posted 01 October 2010 - 06:23 AM

View Postsimeesta, on 01 October 2010 - 04:57 AM, said:

:code:
Thats a lot of code you want explaining. Perhaps ask a specific question, and when that's answered, ask another.

How about i start off with the following

fgets(buff, 255, infile);
if (strlen(buff) <= 10)
break;
pch = strtok(buff, ",");
(*r).transType = pch[0]; /* transtype */
pch = strtok(0, ",");
strcpy((*r).seriesID, pch); /* seriesID */
pch = strtok(0, ",");
if (counter % 2 == 0)/* productID */
sprintf((*r).productID, "%d", counter);

This post has been edited by JackOfAllTrades: 01 October 2010 - 07:34 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: text to Binary understanding

Posted 01 October 2010 - 06:34 AM

So just ignore the :code: then. edit your post and add code tags. it's not difficult.

fgets is a function that reads from a filestresm - infile. it will read 255 character or until a newline is reached. and it will store it int the arrray buff.

If you used google you could have found that out for yourself.

strlen();

i'm sure you can search the rest.

(*r).transType. I can understand why this may seem confusing. r is a pointer to a struct and transType is a member of r. to access transtype you must access it through the pointer by using a '.'.

Some tutorials to help you:
Structures
Pointers

This post has been edited by simeesta: 01 October 2010 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1