Getting numbers from a text file with C

Getting numbers from a text file with C

Page 1 of 1

3 Replies - 1073 Views - Last Post: 19 October 2010 - 11:18 AM Rate Topic: -----

#1 Guest_Master-C*


Reputation:

Getting numbers from a text file with C

Posted 19 October 2010 - 08:46 AM

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

Is This A Good Question/Topic? 0

Replies To: Getting numbers from a text file with C

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Getting numbers from a text file with C

Posted 19 October 2010 - 08:53 AM

We are happy to help you with your good faith efforts. Post your code using code tags, as well as your problems or errors. If you do not know where to begin, you should check out the C/C++ Tutorials Section.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Master-C*


Reputation:

Re: Getting numbers from a text file with C

Posted 19 October 2010 - 08:56 AM

We can only read from this file but I don't know how to separate the numbers so this is the code for now

#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;
}


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

:code:

This post has been edited by JackOfAllTrades: 19 October 2010 - 09:09 AM

Was This Post Helpful? 0

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • Joined: 30-May 10

Re: Getting numbers from a text file with C

Posted 19 October 2010 - 11:18 AM

Use fgets() to read each line.

Use sscanf() to parse two integers and an operator from the buffer in memory.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1