Thread: Reading vertically from a data

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    23

    Reading vertically from a data

    Hi ppl. Beginner here..

    I have a set of data

    1 2 3 4 5 6 7 8 9
    9 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8 9

    how do i use a fscanf statement to read the third number in the second row(which is 7 as highlighted)? R is the variable I use to store the read value. And if I want to replace it with some other number, for example, get input from the user and replace the number 7 with 5, how do I do it? Below is my code. Thanks!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
    	FILE* fx= fopen("cool.txt","r");
    
    	int i,j,R;
    	
    	for(i=1;i<3;i++)
    	{		
    		for(j=1;j<4;j++)
    			fscanf(fx,"%d ",&R); 
    	}
    	fclose(fx);
    	printf("R is %d ", R);
    
    	return 0; 
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    There's no need for a pair of nested loops when it can all be done using fscanf().
    Lookup the scanset feature of fscanf() and how to skip unwanted fields with "*".

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    my teacher want me to use the for loops.. haha Because we are just starting out on C programming. we haven't learnt skipping unwanted fields, he allows us only to use the stuff we learnt.. pls help!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you can't replace anything, if you're opening in read mode. Anyway, you have to read the entire line before you can get to the second line. So you need to read a whole line, then start counting out as you read on the second line.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    int dummy;
    scanf("%d %d %d", &dummy, &dummy, &num);

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two loops, roughly:
    Code:
    one for loop for rows  {
       fscanf("%d %d %d", &skip, &skip, &skip);  //add %d and &skip for each column in a row
       one for column on the right row
       for(i = 1, 1 < 4; i++) {
          if(i == 3)
            fscanf("%d", &myNumber);
          else
            fscanf("%d", &skip);
      }
    }
    I wouldn't mess with fscanf()'s scanset features - they're a bit of a mess, to put it mildly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM
  2. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  3. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 02:27 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Binary data reading
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 06:56 PM