Thread: Storing # from Data File in Arrays?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    Storing # from Data File in Arrays?

    Hi I was wondering if it was possible to store a data file in an array:

    The data file (vec_in.dat) would look like this :
    Code:
    1.2 -2.3 0.53 2.01 -1.5
    I'm trying to store these 5 numbers in an array called v[5]. I want to manipulate the data in v[5] and spit out a product stored in vec[5]. Then I want to print the output onto a screen and a data file.

    This is my code so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(){
    int k;
    double text[81], v[5], vec[5];
    double norm, mid;
    FILE *Inf_1, *Outf_1;
    Inf_1 = fopen("vec_in.dat", "r");
    Outf_1 = fopen("vec_out.dat", "w");
    if ((Inf_1 == NULL) || (Outf_1 == NULL))
    {
    printf("Error oepning the file\n");
    exit(1);
    }
    while(fgets(text,81,Inf_1)!=NULL)
      {  
    sscanf(text, "%lf", &v);
    for (k=0; k<=5; k++) {
    mid += pow(v[k],2);
    }
    norm = sqrt(mid);
    for (k=0; k<=5;k++) {
    vec[k] = v[k]/norm;
    }
    }
    printf("\nOriginal Vector = [");
    printf("%.6f", v[k]);
    printf(" ]\n");
    printf("\nNormalized Vector = [");
    printf("%.6f", vec[k]);
    printf(" ]\n");
    
    fprintf(Outf_1,"\nOriginal Vector = [");
    fprintf(Outf_1,"%.6f", v[k]);
    fprintf(Outf_1," ]\n");
    fprintf(Outf_1,"\nNormalized Vector = [");
    fprintf(Outf_1,"%.6f", vec[k]);
    fprintf(Outf_1,"\n");
    
    fclose(Inf_1);
    fclose(Outf_1);
    exit(0);
    }
    Currently, I'm getting these errors:
    15 q3.c
    warning: passing arg 1 of `fgets' from incompatible pointer type
    17 q3.c
    warning: passing arg 1 of `sscanf' from incompatible pointer type

    Am I using the fgets correctly? Is v[k] being properly converted to vec[k]?

  2. #2
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    You have at least 4 problems in your code.
    1. Never use literal value as the size of array, using sizeof instead.
    Code:
    while(fgets(text, sizeof(text), Inf_1)!=NULL)
    2. text is an array of double, but fgets and sscanf need a pointer to char.
    Code:
    while(fgets(text,81,Inf_1)!=NULL) {
    ...
    sscanf(text, "&#37;lf", &v);
    You should change the definition of array text from double to char.
    Code:
    char text[81];
    3. You are using += operator, but variable mid has never initialized, as of it's a local variable, its initialized value isn't 0.
    Code:
    mid = 0;
    ...
    mid += pow(v[k],2);
    4. function exit is defined in stdlib.h, you should add it in your program.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Those tips are great. Thank you very much.

    My code is compiling but my vectors aren't being stored correctly. This is my output:

    iacs5.ucsd.edu% a.out

    Original Vector = [2.000000 ]

    Normalized Vector = [0.000000 ]

    I want my output to look like this:

    Original Vector = [ 1.200000 -2.300000 0.530000 2.010000 -1.500000 ]

    Normalized Vector = [ 0.329033 ............................. -0.411291 ]

    This is my edited code (able to compile):
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main(){
    int k;
    double v[5], vec[5];
    char text[81];
    double norm, mid = 0;
    FILE *Inf_1, *Outf_1;
    Inf_1 = fopen("vec_in.dat", "r");
    Outf_1 = fopen("vec_out.dat", "w");
    if ((Inf_1 == NULL) || (Outf_1 == NULL))
    {
    printf("Error oepning the file\n");
    exit(1);
    }
    while(fgets(text,sizeof(text),Inf_1)!=NULL)
      {  
    sscanf(text, "%lf", &v);
    for (k=0; k<=5; k++) {
    mid += pow(v[k],2);
    }
    norm = sqrt(mid);
    for (k=0; k<=5;k++) {
    vec[k] = v[k]/norm;
    }
    }
    printf("\nOriginal Vector = [");
    printf("%.6f", v[k]);
    printf(" ]\n");
    printf("\nNormalized Vector = [");
    printf("%.6f", vec[k]);
    printf(" ]\n");
    
    fprintf(Outf_1,"\nOriginal Vector = [");
    fprintf(Outf_1,"%.6f", v[k]);
    fprintf(Outf_1," ]\n");
    fprintf(Outf_1,"\nNormalized Vector = [");
    fprintf(Outf_1,"%.6f", vec[k]);
    fprintf(Outf_1,"\n");
    
    fclose(Inf_1);
    fclose(Outf_1);
    exit(0);
    }
    ... and this is the input file I want it to read (vec_in.dat):
    Code:
    1.2 -2.3 0.53 2.01 -1.5
    Why is the array only returning one value?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    printf("%.6f", v[k]);
    And you expect this to print more than one number because why?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    It seems the print only v[0] if I explicitly tell it to print v[0], v[1], v[2] etc

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main(){
    int k;
    double v[6], vec[6];
    char text[81];
    double norm, mid = 0;
    FILE *Inf_1, *Outf_1;
    Inf_1 = fopen("vec_in.dat", "r");
    Outf_1 = fopen("vec_out.dat", "w");
    if ((Inf_1 == NULL) || (Outf_1 == NULL))
    {
    printf("Error oepning the file\n");
    exit(1);
    }
    printf("\nOriginal Vector = [");
    while(fgets(text,sizeof(text),Inf_1)!=NULL)
      {
    sscanf(text, "&#37;lf", &v);
    for (k=0; k<=5; k++) {
    printf("%.6f ", v[k]);
    mid += pow(v[k],2);
    }
    norm = sqrt(mid);
    for (k=0; k<=5;k++) {
    vec[k] = v[k]/norm;
    }
    }
    printf(" ]\n");
    printf("\nNormalized Vector = [");
    printf("%.6f", vec[k]);
    printf(" ]\n");
    
    fprintf(Outf_1,"\nOriginal Vector = [");
    fprintf(Outf_1,"%.6f", v[k]);
    fprintf(Outf_1," ]\n");
    fprintf(Outf_1,"\nNormalized Vector = [");
    fprintf(Outf_1,"%.6f", vec[k]);
    fprintf(Outf_1,"\n");
    
    fclose(Inf_1);
    fclose(Outf_1);
    exit(0);
    }
    Output:
    iacs5.ucsd.edu% gcc q3.c -lm
    iacs5.ucsd.edu% a.out

    Original Vector = [1.200000 0.000000 0.000000 0.000000 0.000000 0.000000 ]

    Normalized Vector = [1.200000 ]

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want a statement to be repeated more than once, you must use a looping construct of some kind. Since you aren't, it won't be.

  7. #7
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    Ah, you have three problems in your code still.
    1. I have no idea about if there is a convenient way to use array with sscanf, the ugly way here is :
    Code:
    sscanf(text, "&#37;lf %lf %lf %lf %lf", v, v+1, v+2, v+3, v+4);
    Remeber, array is pointer and pointer is array on some occasions. So, you needn't dereference an array identifier when you need a pointer to the array.

    2. array index starts with 0 nor 1. so there are problems within you loop structure.
    Code:
            for (k=0; k<5; k++) {
                mid += pow(v[k],2);
            }
            norm = sqrt(mid);
            for (k=0; k<5;k++) {
                vec[k] = v[k]/norm;
            }
    3. the third is what tabstop said, you need adding a loop structure to print all elements of an array.
    Code:
    		printf("\nOriginal Vector = [");
    		for (k = 0; k < 5; k++) {
    			printf("%.6f ", v[k]);
    		}
    		printf(" ]\n");
    		printf("\nNormalized Vector = [");
    		for (k = 0; k < 5; k++) {
    			printf("%.6f ", vec[k]);
    		}
    		printf(" ]\n");
    below is the code I have modified, it generates the output what you want.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    int main(){
        int k;
        char text[81];
        double v[5], vec[5];
        double norm, mid;
        FILE *Inf_1, *Outf_1;
    
    	mid = 0;
    	Inf_1 = fopen("vec_in.dat", "r");
    	Outf_1 = fopen("vec_out.dat", "w");
        if ((Inf_1 == NULL) || (Outf_1 == NULL))
        {
            printf("Error oepning the file\n");
            exit(1);
        }
        while(fgets(text,81,Inf_1) != NULL)
        {  
            sscanf(text, "%lf %lf %lf %lf %lf", v, v+1, v+2, v+3, v+4);
            for (k=0; k<5; k++) {
                mid += pow(v[k],2);
            }
            norm = sqrt(mid);
            for (k=0; k<5;k++) {
                vec[k] = v[k]/norm;
            }
        }
    
    		printf("\nOriginal Vector = [");
    		for (k = 0; k < 5; k++) {
    			printf("%.6f ", v[k]);
    		}
    		printf(" ]\n");
    		printf("\nNormalized Vector = [");
    		for (k = 0; k < 5; k++) {
    			printf("%.6f ", vec[k]);
    		}
    		printf(" ]\n");
    	    
    		fprintf(Outf_1,"\nOriginal Vector = [");
    		for (k = 0; k < 5; k++) {
    			fprintf(Outf_1,"%.6f", v[k]);
    		}
    		fprintf(Outf_1," ]\n");
    
    		fprintf(Outf_1,"\nNormalized Vector = [");
    		for (k = 0; k < 5; k++) {
    			fprintf(Outf_1,"%.6f", vec[k]);
    		}
    		fprintf(Outf_1,"\n");
    
        
        fclose(Inf_1);
        fclose(Outf_1);
    	system("pause");
        exit(0);
    }
    Last edited by bbebfe; 11-13-2008 at 09:10 PM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Problem solved! I defined the variables in the "ugly way" at first but I tried to find a more elegant solution. I tried putting sscanf in the k loop but I didn't know how to stop it from scanning the same number over and over. I guess I have to settle. Thanks!
    Last edited by dakarn; 11-13-2008 at 09:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  3. Skipping records to text file and storing data.
    By Mr. Deeds in forum C Programming
    Replies: 5
    Last Post: 09-10-2007, 11:24 PM
  4. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM