Thread: read from file and put into double array ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    read from file and put into double array ...

    i got stuck on another subject here:

    i got a file of format:

    1;Name One
    2;Name Two
    etc...

    i have to read that, and put it into:

    the numeric part in an int array1[x]
    and the name part into char array2[x][y]

    so this way i can reference from array1 to array2... it's not only a double array, it's also paralel arrays.

    now i got this and it doesn't work right... i can't find a symilar example anywhere:

    Code:
    void prep_product_report(){
    	FILE *fp;
    	int i;
    	int prod_codes[12];
    	char prod_desc[12][30];
    	int salesman_no[10];
    	char salesman_name[10][30];
    	
    	if((fp = fopen("salespeople.dat", "r")) == NULL)
    		printf("Can not open salespeople.dat file.\n");
    	else{
    		rewind(fp);
    		for(i = 0; i<10; i++){
    		  fscanf(fp, "%d;%s", salesman_no, salesman_name[i]);
    	  }
    	  fclose(fp);
      }
    
    	if((fp = fopen("products.dat", "r")) == NULL)
    		printf("Can not open products.dat file.\n");
    	else{
    		rewind(fp);
    		for(i = 0; i<12; i++){
    			fscanf(fp, "%*d;%s", prod_codes, prod_desc[i]);
    		}
    	fclose(fp);
    	}
    
      product_report(prod_codes, prod_desc, salesman_no, salesman_name);
    }
    problem is i am forced to create a function declared:

    Code:
    void product_report(int prod_codes[ ], char prod_desc[ ][30], int  salesman_no[ ], char salesman_name[ ][30]);
    any help much appreciated.

    Thank you!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> fscanf(fp, "%d;%s", salesman_no, salesman_name[i]);

    Shouldn't that be:

    >> fscanf(fp, "%d;%s", &salesman_no[i], salesman_name[i]);

    Ditto for the other loop.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    it only puts a character in there (not even the first char). It's not reading the whole string. And the salesman one isn't reding anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fscanf(fp, "%*d;%s", prod_codes, prod_desc[i]);
    Well the * is assignment suppression. You read an int, but it isn't written anywhere.

    Also, add some error checking and use a debugger.

    int num = fscanf(fp, "%d;%s", &prod_codes[i], prod_desc[i]);
    if ( num != 2 ) // panic!!!
    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.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    ok, it's not reading right (or rather it's not assigning)... is that how you use fscanf to read more lines? does fscanf read from where it left off last time? now i'm getting even more confused.

    yead, the * was left in there from another test i made, it's a mistake.... and i added the &.

    yet, a simplified test like this works:

    Code:
    main(){
    	char arry[2][30];
    	int arry2[2];
    
    	printf("Enter name(dd;ccc):");
    	scanf("%d;%s", &arry2[0], &arry[0]);
    	
    	printf("%s \n", arry[0]);
    	printf("%d \n", arry2[0]);
    }
    am i not doing the same thing (except reading from file instead of stdin)?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is that how you use fscanf to read more lines?
    It will pick up from the end of the previous fscanf() call.
    So if you just have a file containing
    12 hello
    34 world

    kinda thing, then it will read those as you would expect by calling fscanf() in a loop with a "%d %s" format
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM