open file with numbers and multiply them

This is a discussion on open file with numbers and multiply them within the C Programming forums, part of the General Programming Boards category; i have large ascii files that i need to open and multiply each number on it by a factor how ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    28

    open file with numbers and multiply them

    i have large ascii files that i need to open and multiply each number on it by a factor

    how will i be able to implement this?
    will i have to store the numbers in a variable and then multiply it? and how can i output the result to an ascii file looking the same as the original one?

    the data is like this:

    -4562.16 741.974 -3552.31
    -4559.26 742.236 -3550.07
    -4548.94 739.526 -3530.89

    (xyz)
    I have looked for this but i don`t know how to do it.

    any ideas would be great.
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Begin with a program which simply reads the input file, and prints the converted numeric data back to you. Until you can do that, the rest of the problem is inaccessible.
    Some ideas for doing this are in the FAQ.

    When the input is good, then focus on your next task.

    Post here when you get stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by autopilot View Post
    I have looked for this but i don`t know how to do it.
    Surely you must at least learned how to open a file at school and how to read in numbers. And yes, you *will* need at least one variable :-S

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    28

    Wink

    I wrote a few lines but the result is not what i would expect. Here is how it is atm
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float x;
    float y;
    float z;
    char line[200];
       
    int main() {
    	FILE *in, *out; 	
        in = fopen( "dados.txt", "r" ); /* abre o ficheiro dados.txt para leitura e associa-o a in */
        if( in == NULL ) {
        	printf("ERRO: não consigo abrir o ficheiro dados.txt\n");
            exit(1);
            }
        
        out = fopen( "resultados.txt", "w" ); /* abre o ficheiro resultados.txt para escrita e associa-o a out */
    	if( out == NULL ) {
    		printf("ERRO: não consigo abrir o ficheiro resultados.txt\n");
    		exit(1);
    		}
    	
    	/* leitura e escrita */ 
    	fgets(line, sizeof(line),stdin);
    	sscanf(line, "%f,%f,%f", &x, &y, &z);
    	printf("%f,%f,%f\n", x,y,z);
    	fprintf(out, "%f,%f,%f\n", x*2, y*2, z*2);
        /* fecha os ficheiros */
        fclose( in );
        fclose( out );
        return (0);
        }
    It compiles fine, but the result is
    Code:
    sh-3.2$ ./a.out 
    
    0.000000,0.000000,0.000000
    resultados.txt looks like this "0.000000,0.000000,0.000000"
    only one line is computed and the result is 000...
    i need a few tips on do the multiplication and continue it till the end of the file...
    thanks for the motivation!

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Try reading from the file in question and not stdin.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by MacGyver View Post
    Try reading from the file in question and not stdin.
    Thanks
    changed to
    Code:
    fgets(line, sizeof(line),in);
    now the result is
    -9124.320312,1483.947998,-7104.620117

    But it only reads one line..What do i need to change to make the
    Code:
    	fgets(line, sizeof(line),in);
    	sscanf(line, "&#37;f,%f,%f", &x, &y, &z);
    	fprintf(out, "%f,%f,%f\n", x*2, y*2, z*2);
    go trough all the lines of the input file?

    And also what to change to make use the original number of decimal places?
    Last edited by autopilot; 09-06-2007 at 06:40 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    1. Put those lines in a loop.
    2. The *printf() family of functions allow you to specify how many decimal places to use. Google for "printf format specifers"

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    28

    almost there

    i am almost done, thanks a lot for the help friends.

    i tried
    Code:
    //while (!feof(in)) { 
    	fgets(line, sizeof(line),in);
    	sscanf(line, "&#37;f,%f,%f", &x, &y, &z);
    	fprintf(out, "%f,%f,%f\n", x*2, y*2, z*2);
    	}
    and this works except for the fact that the last line of "in" is computed and printed to out twice.

    if i use
    Code:
    while (fgets(line, sizeof(line), in) != NULL) {
    	fgets(line, sizeof(line),in);
    	sscanf(line, "%f,%f,%f", &x, &y, &z);
    	fprintf(out, "%f,%f,%f\n", x*2, y*2, z*2);
    	}
    the FIRST line is ignored
    Last edited by autopilot; 09-06-2007 at 06:58 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    I have seen that link thanks. But using the sugestion the first line of the file is not computed or at least won`t go to the output file.
    Code:
    while (fgets(line, sizeof(line), in) != NULL) { 
    	fgets(line, sizeof(line),in);
    	sscanf(line, "%f,%f,%f", &x, &y, &z);
    	fprintf(out, "%f,%f,%f\n", x*1, y*1, z*1);
    	}

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you call fgets twice. why? That means that you only compute every other line.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by robwhit View Post
    you call fgets twice. why? That means that you only compute every other line.
    OF course! Thanks a lot, all done know
    I Love this board. It helps a lot by pointing out the ways where we should go.

    Peace

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you multiply it by 1 you're not going to see any results...
    Consider:
    Code:
    while(fgets(line, sizeof(line), in) != NULL)
    { 
        sscanf(line, "&#37;f,%f,%f", &x, &y, &z);
        fprintf(out, "%f,%f,%f\n", x * 2.0f, y * 2.0f, z * 2.0f);
    }

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by zacs7 View Post
    If you multiply it by 1 you're not going to see any results...
    Consider:
    Code:
    while(fgets(line, sizeof(line), in) != NULL)
    { 
        sscanf(line, "%f,%f,%f", &x, &y, &z);
        fprintf(out, "%f,%f,%f\n", x * 2.0f, y * 2.0f, z * 2.0f);
    }
    I know it was just for testing... i am planning to ask for the multiplication factor!

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplying Huge numbers in integer arrays
    By invierno in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 09:40 PM
  2. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 06:21 PM
  3. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 05:03 PM
  4. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 09:41 PM
  5. data in a file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-05-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21