Thread: changing output with fprintf

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    changing output with fprintf

    I have an input file that contains lines

    Code:
    word1 word2 word3 45 78 90
    word3 word4 word5 45 80 100
    My program read its lines and displays them in another file using:

    Code:
     fprintf(inptr2, "%s", buffer);


    So far the lines in the output file are exactly the same, but I want to change te contents of that written line, I would like to change all the letters to all upper case and diplay the average of the 2 last numbers.

    to obtain something like this in the new file:

    Code:
    WORD1 WORD2 WORD3 90
    any ideas ?

    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For each word, for each letter, put it to uppercase. For the last two numbers, add them together and then divide by 2.

    Ask a specific question to receive a specific answer.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Quote Originally Posted by MacGyver View Post
    For each word, for each letter, put it to uppercase. For the last two numbers, add them together and then divide by 2.

    Ask a specific question to receive a specific answer.

    all right thanks for your help (the keyword was to uppercase)

    I made a for loop such as follow:
    Code:
    for (i=1; i < sizeof buffer; i++)
    {
         buffer[i] = toupper(buffer[i]);
    }
    it worked just fine.

    But for what to do to make an average of the last two numbers of each line ?

    (number 1 + number 2 ) / 2;

    for exemple the last two numbers of the first line "78" and "90", I want to add them up and average them and show the results in the output file... I want to do it in such a way it would work for all other lines ...

    Is there anyway to "designate" those two numbers to program in order to make the calculations ?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Um, you could store the numbers as well as the words in variables using your sscanf statement. sscanf works like the other members of the *scanf family, but the input is from a string.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    any coding example, or tutorial would be welcome.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This may be of interest.

    You could use sprintf instead of printf for each text field. If you have an index less than 4, you could fprintf to the output file. For higher indexes you then could convert the incoming text to a number and add it to the current sum. Then, when the for loop completes, you fprintf the sum.

    That and other tweaks could produce this:
    word1 word2 word3 45 168
    word3 word4 word5 45 180
    Adding the uppercase-ing of the text is not that difficult.

    [edit]The averaging is not that difficult, and a final output of the following follows from making incremental changes.
    WORD1 WORD2 WORD3 45 84
    WORD3 WORD4 WORD5 45 90
    This code is even extensible to take more than 2 values for averaging.


    But is important that you know what you are doing, so don't just copy-and-paste. Post questions along the way about stuff there that you don't understand, and post questions along the way about making the modifications.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    I am having hard time testing this function, can you please show me how to use the sprintf and make the average ?

    here's my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define   FILENAME   "input.dat"
    
    void FileProcess(void);
    
    int main ( void )
    #include <string.h>
    #define   FILENAME   "input.dat"
    
    void FileProcess(void);
    
    int main ( void )
    {
    char fname [21];
    
    printf("Enter file name: ");
    scanf("&#37;s", fname);
    
    if (strcmp ( fname, FILENAME ))
    {
       printf("Unable to open file specified!\n");}
    
       else
       {
            FileProcess();
        }
    
    }
    
    void FileProcess (void)
    {
          FILE * fp, * inptr, * inptr2;
    
          char buffer [81];
          int i, count=1;
          int count2=1;
          int x;
    
          fp = fopen ( FILENAME, "r" );
          inptr = fopen ( "east.dat", "w");
          inptr2 = fopen ( "west.dat", "w");
          x = 1;
    
         while ( fgets( buffer, sizeof buffer, fp ) != NULL )
         {
               fputs( buffer, stdout );
    
               for ( i = 0; i < sizeof buffer; i++)
               {
                    buffer[i] = toupper(buffer[i]);
               }
    
               if (x==1)
              {
                 x = 0;
                 fprintf(inptr, "Roster#%i - %s", count++, buffer);
               }
    
               else
               {
                   x=1;
                   fprintf(inptr2, "Roster#%i - %s", count2++, buffer);}
               }
    
                    fgets(buffer, 81, fp);
       fclose(fp);
       fclose(inptr);
       fclose(inptr2);
    }

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Maybe you could try and deal with the file one line at a time? Then approach your two tasks (uppercasing and averaging) separately.

    Code:
    while not end of file
    {
       get 3 words and 3 numbers
       uppercase each letter
       find average of numbers
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  2. Changing or updating output
    By IdioticCreation in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2006, 11:10 AM
  3. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  4. Changing Output
    By Jripeus in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2002, 06:16 PM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM