fscanf in loop

This is a discussion on fscanf in loop within the C Programming forums, part of the General Programming Boards category; well i am having a strange problem it goes as like this Code: //for(int i=1;i<n+1;i++); fscanf(fp,"%d %s",&e[1].p_len,e[1].data); cout<<e[1].p_len<<e[1].data; /* for( ...

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

    Angry fscanf in loop

    well i am having a strange problem it goes as like this

    Code:
    //for(int i=1;i<n+1;i++);
    	fscanf(fp,"%d %s",&e[1].p_len,e[1].data);
    	cout<<e[1].p_len<<e[1].data;
    /*	for( i=1;i<n+1;i++)
    		printf("%d %s",e[i].p_len,e[i].data)*/
    the above code works fine but when i do this that is applying the loop on fscanf
    Code:
    for(int i=1;i<n+1;i++);
    	fscanf(fp,"%d %s",&e[i].p_len,e[i].data);
    	cout<<e[1].p_len<<e[1].data;
    /*	for( i=1;i<n+1;i++)
    		printf("%d %s",e[i].p_len,e[i].data)*/
    i get segmentation fault error

    my entire code is here
    Code:
    struct extract
    {
    	int p_num;
    	int p_len;
    	char data[100];
    };
    using namespace std;
    int main (int argc , char *argv[]){
    	FILE *fp;
    	struct extract *e;
    	int n,i;
    	fp=fopen("data.raw","r");
    	fscanf(fp,"%d",&n);
    	e = new extract[n+1];
    	e[0].p_num=n;
    	cout<<n;
    	for(int i=1;i<n+1;i++);
    	fscanf(fp,"%d %s",&e[i].p_len,e[i].data);
    	cout<<e[1].p_len<<e[1].data;
    /*	for( i=1;i<n+1;i++)
    		printf("%d %s",e[i].p_len,e[i].data)*/
    	return 0;
    }
    thanks in advance...

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You are missing a "&" operator in the fscanf call. You need to pass the address of the variable to which the data is to be stored, not the variable itself.

    You have this:
    Code:
    fscanf(fp,"&#37;d %s",&e[i].p_len,e[i].data);
    You need this:
    Code:
    fscanf(fp,"%d %s",&e[i].p_len,&e[i].data);
    Edit:
    btw, your code is a little confusing since it contains elements of C and C++ (the cout statements are C++). As a general rule, it's best to stick to one or the other when you write a program.
    Last edited by Swarvy; 11-08-2008 at 03:51 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Quote Originally Posted by Swarvy View Post
    You are missing a "&" operator in the fscanf call. You need to pass the address of the variable to which the data is to be stored, not the variable itself.

    You have this:
    Code:
    fscanf(fp,"%d %s",&e[i].p_len,e[i].data);
    You need this:
    Code:
    fscanf(fp,"%d %s",&e[i].p_len,&e[i].data);
    No use swarvy it is the same even if i add '&' before it.. i get the same error during the run time..
    thanks for the reply anyhow

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > for(int i=1;i<n+1;i++);
    1. The local i is different from your global i (which remains uninitialised)
    2. The ; at the end means this loop does nothing.

    > fscanf(fp,"&#37;d %s",&e[i].p_len,e[i].data);
    This does on the other hand use the aforementioned uninitialised i, and trashes some random location.
    You were doing better in your other post.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Quote Originally Posted by Salem View Post
    > for(int i=1;i<n+1;i++);
    1. The local i is different from your global i (which remains uninitialised)
    2. The ; at the end means this loop does nothing.

    > fscanf(fp,"%d %s",&e[i].p_len,e[i].data);
    This does on the other hand use the aforementioned uninitialised i, and trashes some random location.
    You were doing better in your other post.
    thanks salem....

    _________________________________________________
    how could i be so foolish to oversee a small mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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