-1

Suppose I wanted to read in and multiply number 1 by number 4

5000     49     3.14     Z      100
0322     35     9.21     X      60

Currently I have, but am only able to copy information not manipulate the information

#include <stdio.h>
#include <stdlib.h>
#define FILE_1 "File1.txt"
#define FILE_2 "File2.txt"
int main (void)
{
    // Local Declarations 
    char score;
    int curCh;
    int count = 0;
    FILE* sp1;
    FILE* sp2;

    if (!(sp1 = fopen (FILE_1, "r"))) //check if file is there
    {
        printf ("\nError opening %s.\n", FILE_1);
        return (1);
    } // if open error 
    if (!(sp2 = fopen (FILE_2, "w")))
    {
        printf ("\nError opening %s.\n", FILE_2);
        return (2);
    } // if open error

    while((curCh = fgetc(sp1)) != EOF)
    {
        printf ("%c", curCh); //copy the contents
            count++;
    } // while 


    return 0;
}
user1530249
  • 1,047
  • 3
  • 19
  • 28
  • 1
    You tag fcsnf but don't even use it in your code. That said, you may find that fgets() and sscanf() provide a cleaner way to parse this type of data. – Randy Howard Mar 15 '13 at 01:26
  • @RandyHoward Perhaps, if the file is stdin. When the file is another file, fscanf usually isn't a problem because the input isn't so irregular. – autistic Mar 15 '13 at 02:10
  • Where is your question? – autistic Mar 15 '13 at 02:11
  • 1
    Given the data, you should read whole lines with `fgets()`, and then parse each line with `sscanf()`, checking that you did read data and that you got the correct number of values converted. You might note that `stderr` is the standard error channel; it is where you should write messages about problems such as failing to open files. Don't forget to close the files when you're done. – Jonathan Leffler Mar 15 '13 at 02:13

2 Answers2

1

Agree with Randy's and Jonathan's comments that you should use fgets() to handle entire lines. If you have known delimiters (like tab) and known columns, you could use strtok() to tokenize your line on the delimiter and then use the count to pull the values you want.

In addition to sscanf(), you might be able to get away with atoi() and atof() make successful use of strtol() as noted in Randy's comment below, and referenced elsewhere on StackOverflow:

Community
  • 1
  • 1
SeKa
  • 1,825
  • 12
  • 7
  • I mentioned using fgets()/sscanf() in a comment an hour ago, but since then he has modified his question several times for minor things, but still has made no attempt to parse the data himself. He seems to want SO to do it for him. atoi() and atof() have both been deprecated. Look at strtol()/strtod(), etc. – Randy Howard Mar 15 '13 at 02:32
  • @RandyHoward Sorry I missed your first comment; Edited my post to reflect your input re: atoi/atof. – SeKa Mar 15 '13 at 08:31
0

Multiplying 1 by 4 is easy: 1 * 4.

Do you mean "multiply better_identifier by best_identifier, uint64_t values read from the same file"? What is the best identifier you can come up with?

You'll need these #includes:

#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <inttypes.h>

Don't forget to comment this out:

/*while((curCh = fgetc(sp1)) != EOF)
{
    printf ("%c", curCh); //copy the contents
        count++;
}*/ // Make sure you comment this, because the side-effect of this
    // ... won't allow you to do anything else with sp1, until you
    // ... rewind

Which book are you reading, by the way?

uint64_t better_identifier = 0, best_identifier = 0;
assert(fscanf(sp1, "%"SCNu64" %*d %*g %*c %"SCNu64, &better_identifier, &best_identifier) == 2);
printf("%"PRIu64" * %"PRIu64" = %"PRIu64"\n", better_identifier, best_identifier, better_identifier * best_identifier);

Perhaps you meant to use x and y as identifiers. Surely you can come up with better identifiers than that!

uint64_t x = 0, y = 0;
assert(fscanf(sp2, "%"SCNu64" %*d %*g %*c %"SCNu64, &x, &y) == 2);
printf("%"PRIu64" * %"PRIu64" = %"PRIu64"\n", x, y, x * y);
autistic
  • 1
  • 3
  • 35
  • 80