Thread: open file with numbers and multiply them

  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
    39,660
    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.

  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,210
    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 05: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,210
    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 05:58 PM.

  10. #10

  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!

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, 08: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, 05:21 PM
  3. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  4. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 08:41 PM
  5. data in a file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-05-2002, 02:23 PM