Thread: positve and negative

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    positve and negative

    Hi can anone explain how text files store positive and negative values

  2. #2
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile

    If the data structure of the file data is known, successive strings can be read from the text file using fscanf(). Each %d in the function call will account for a minus sign and store a negative as required. There must be no gap between the minus and the "attached" integer. Here's something I came up with:

    Copy and paste the sample data onto an A: disk, taking care to preserve the spaces and end-of-lines. Name the file "SAMPLE.DAT"... The imaginary data structure consists of five integers (+/-) as declared in my program.

    Data is:

    32 20 -15 73 -2
    70 -11 6 8 -3
    44 -6 -5 7 200
    45 -1 22 84 1
    -9 30 -55 -425 -34
    220 465 -1000 -9990 7260

    (final item to be followed by newline)

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define LIMIT 20
    
    struct data{int a, b, c, d, e;};
    
    int main()
    {
     int i=0, rec_count=0;
     FILE *f_ptr;
    
    struct data record[LIMIT]; /* In practice you would need to use malloc
                                  for unknown/large numbers of file records.
             This program will work for up to 20 records of 5 integers each. */
    
    if((f_ptr=fopen("A:SAMPLE.DAT","rt"))==NULL)
     {
      printf("CANNOT OPEN FILE TO READ - PROGRAM STOPPED!\n\n"
    	 "PLEASE ENSURE DISK IS PRESENT IN A:DRIVE!\a");
      exit(1);
      }
    
    /* Read text file lines until EOF */
    
    while((fscanf(f_ptr,"%d %d %d %d %d\n",&record[i].a,&record[i].b,
                              &record[i].c,&record[i].d,&record[i].e))!=EOF)
     {
      rec_count++;
      i++;
      }
    
    /* Output the file data which is now in the array of structures in RAM
       Format the screen output for better presentation */
    
    printf("RECORDS FROM FILE A:SAMPLE.DAT ARE AS FOLLOWS:\n\n");
    
    for(i=0; i<rec_count; i++)
        printf("REC %d: %5d %5d %5d %5d %5d\n",i+1, record[i].a, record[i].b,
                                       record[i].c, record[i].d, record[i].e);
    
    printf("\n[END OF RECORDS]\n\n* * * END OF PROGRAM!! * * *\a");
    
    fclose(f_ptr);
    
    return 0;
    } /* end of main */
    Good luck from MisterBadger!!

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Thx

    Thx 4 the post.
    I am still a little unsure. Perhaps I am asking for the wrong info?

    I have written a sample program to read and validate text files. I have also to produce test data. I have been given example data and in the required field the examples
    +000125436
    +000000527
    -000002123 etc.

    a note below.
    The + and - signs shown in the fourth field are for reference only.
    This is not how a signed value is held in memory???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little maths problem for you
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 181
    Last Post: 06-26-2002, 12:56 PM