Reading file..Saving data in new file... 
Author Message
 Reading file..Saving data in new file...

I'm working on a large assignment requiring reading then writing to
files..

lets say i want to make an adding program...

THE (SAVED AS ADD.IN) INPUT FILE READS:
1
+
1
=
1
+
2
+
1
+
2
=

THE PROGRAMS TAKES THE DATA AND SAVES THE RESULT
IN AN OUTPUT FILE (ADD.OUT) SHOULD THATS READS

1+1=2
1+2+1+2=6

HOW WOULD I GO ABOUT THIS?
would it be more convient to hold this procedure in a function..
lets say i want this program is also to divide, mulitply, etc..
should i have a seperate read-write function for EACH operation?
ANY EXPLANATIONS OR EXAMPLES WOULD ME VERY MUCH APPRECIATED



Thu, 06 Sep 2001 03:00:00 GMT  
 Reading file..Saving data in new file...
Wiseoldman schrieb:

Quote:

> I'm working on a large assignment requiring reading then writing to
> files..

> lets say i want to make an adding program...

[snipped input file description]

Quote:
> THE PROGRAMS TAKES THE DATA AND SAVES THE RESULT
> IN AN OUTPUT FILE (ADD.OUT) SHOULD THATS READS

> 1+1=2
> 1+2+1+2=6

> HOW WOULD I GO ABOUT THIS?
> would it be more convient to hold this procedure in a function..

Hi Wiseoldman,

Yes, it would. You write a function that reads a file line by line. For
each line you have three possibilities: "+" or "-" or an integer number.
Consider this sample code:

  while ( fgets(line, MAX_LINE_LENGTH, inFile) != NULL )
  {
    if ( line[0] == '+' )
    {
       /* You have read a '+' : treat that here. */
       fputc( '+', outFile );
    }
    else if ( line[0] == '=' )
    {
       /* You have read a '=' : treat that here.         */
       /* Lets say you keep your sum in a variable "sum" */
       fprintf( outFile, "=%d\n", sum );
    }
    else
    {
       /* None of the above: it must be a number. */
       num = atoi( line );
       fprintf( outFile, "%d", num );
       /* don't forget to process the number */
    }
  }

That's of course only a framework, a guideline. You'll have to fill in
some of the vital bits.

Quote:
> lets say i want this program is also to divide, mulitply, etc..

Well, you can extend the above if-else chain to accomodate '-' and '*' and
'/' as well. But as soon as you want to introduce operator preceedence it
gets complicated. An expression calculator is not an easy thing to program
and I do not know what level of experience you have.

Quote:
> should i have a seperate read-write function for EACH operation?

No, that's not necessary. But I would use a "switch()" statement instead
of the if-else and a status or mode indicator for the current operation.
For a single level of preceedence you'll need an additional buffer for the
intermediate result.

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Fri, 07 Sep 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Binary file read to New text file save grief

2. Reading a pattern of string from text file and writing it to a new file

3. Movie/Sound files, reading data embedded with another file

4. Load a RT_RCDATA-Type from Resourcefile and save this in a new file

5. write data in file and save it

6. Saving dynamic data structures from memory to file

7. save data to file

8. how to save a rgb file from image data

9. Problem with saving data into a text file in pre-formatted form

10. Saving Complex Data Structures on File

11. TurboC reading clipper saved memory file problems

12. Saving file data to char *

 

 
Powered by phpBB® Forum Software