Getting numbers from a text file with C

Discussion in 'C' started by Master-Nedero, Oct 19, 2010.

  1. Master-Nedero

    Master-Nedero New Member

    Joined:
    Oct 19, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Any Ideas guys ?
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    How about starting with:

    Code:
    int main()
    {
       return(0);
    }
    
    In other words please show a good faith effort of doing your assignment. If we supply you with source code at this point, what will you learn?

    Jim
     
    Last edited: Oct 19, 2010
    shabbir likes this.
  3. kumardashish

    kumardashish New Member

    Joined:
    Nov 8, 2008
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    employee
    Location:
    Bangalore
    first when we store any thing in file (char,string or int) compiler first convert it in char after that store it in file. so u can't directly fetch the data from file and simply do some arithmetic operation.first you have to convert it into integer then perform arithmetic operation.
    i have a clue. go through ASCII table there give the ASCII value of 0,1,2,3,4,5,6....
    try to connect them i mean u have to create a equation to convert char to int.
    OK i make it simple..
    suppose you get a value 6 from file. be attention it is not int type it is char type and your program take ASCII value of 6 means it take 54 and you know that ASCII value of 0 is 48
    just subtract these two ASCII and store it in int type variable.
    Code:
    int a;
    char ch;
    FILE *fp;
    fp = fopen("filename","r");
    ch=fgetc(fp);
    a=ch - '0' ;
    
     
  4. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Why not just use fscanf to retrieve the number, sign, number. Or use fgets to read in a line of text into a cstring then use sscanf to retrieve the number, sign, number sequence from the cstring?

    Jim
     
  5. Master-Nedero

    Master-Nedero New Member

    Joined:
    Oct 19, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for all the help ;)
    I made the program in a little difrent way but I am really thankful for your help :pleased:

    Ned
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice