Getting numbers from a text file with C

This is a discussion on Getting numbers from a text file with C within the C Programming forums, part of the General Programming Boards category; This is a program that I have to make please somebody help me The purpose of the task is to ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Question Getting numbers from a text file with C

    This is a program that I have to make please somebody help me

    The purpose of the task is to implement a program that reads the contents of a text
    file (in.txt) and if it qualifies for an arithmetic expression set out below,
    perform the specified arithmetic operation and record the results in a binary file (out.dat).
    Arithmetic expression must meet the following conditions:
    is enrolled in a row;
    consists of two integer operands separated by the sign of arithmetic operation;
    arithmetic operations are permitted: "+" and "-";
    there is allowed an unlimited number of whitespace (space - '' and
    char - '\ t') before and after the sign for simple operation;
    there is allowed an unlimited number of whitespace at the beginning and end of
    arithmetic expression.
    Example:
    in.txt: "158 + 7"
    out.txt: Contains binary written the number 165


    Thanks



    The code I have written for now is:

    Code:
     #include<stdio.h>
    
    int main()
    {
    FILE *fp;
    int c;
    char h;
    
    fp = fopen("sample.txt", "r");
    if(fp == NULL) {
    printf("Input file not found!");
    return 1;
    }
    
    while (EOF != (c = fgetc(fp)))
    printf("%c", c);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Here's some ideas to get you going. This is not complete code (and I left out the EOF error checking that will be needed in your final application).

    Code:
    int operand1 = 0, operand2 = 0;
    
    while (EOF != (c = fgetc(fp)))
    {
        if(isdigit(c))
        {
            /* This is a digit, and therefore is part of operand1 */
        }
    }
    
    c = fgetc(fp);
    if(c == '+')
    {
        /* This is an addition operation */
    }
    else if(c == '-')
    {
        /* This is subtraction */
    }
    else
    { 
        /* Error, we expected + or - */
    }
    
    while (EOF != (c = fgetc(fp)))
    {
        if(isdigit(c))
        {
            /* This is a digit, and therefore is part of operand2 */
        }
    }
    There are better ways of solving your problem than the code up above, but I wanted to keep it simple.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    An alternative to going through the file character by character and filtering out the useless information from the actual mathematical expression is to use strtok(). I leave it to you to decide which method you prefer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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