GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2004, 01:59
kelly80 kelly80 is offline
New Member
 
Join Date: Apr 2004
Posts: 7
kelly80 is on a distinguished road
Question

problem:retrieve from struct


The program below encounter a problem, which when i print out a set of data retrieve from struct, it give me totally different value..the first fprintf() return me the value that i want(which are *2609_AUS74....), but when i put the fprintf() inside another for loop, it just return me [1] [1].....i have attached the input file in the attachmant...

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

typedef struct {
	char *seq_names;
	char *context[893];
}myStruct;

void main()
{
	FILE *infile;
	FILE *outFile;

	int sampleNo, nukloideNo, win=5, con=3;
	char cont[10];
	char filename[30];
	char sequence[12];
	char *m;
	myStruct seq[16];


	do
	{
   		printf("Please enter the input file name:");
		scanf("%s",filename);

	}while((infile=fopen(filename,"r"))==NULL);

	outFile=fopen("outfile","w");
	if(outFile==NULL)
	{
		 printf("Unable to open output file.");
		 exit(0);
	}

	fscanf(infile,"%d %d %d %d", &sampleNo, &nukloideNo, &win, &con);


	int j;
	int i=-1;


	while(fgets(sequence, 12, infile)!=NULL) { 

		if(strstr(sequence,"*")!=NULL)
			{
			i++;
			seq[i].seq_names=sequence;

			fprintf(outFile,"%s\n",seq[i].seq_names);//first fprintf
			j=0;
		
			}

  		else if(strstr(sequence,"#")!=NULL){

			seq[i].context[j]=sequence;
		             j++;}
	
    }

	for(int k=0;k<=i;k++)
		fprintf(outFile,"%s",seq[k].seq_names );//2nd fprintf


	fclose(outFile);
	printf("\nPress any key to Terminate.");
             getch();

   
	return;
}

could anyone tell me why??and how to resolve this problem??

Any help is appreciated!!
Attached Files
File Type: txt infile1.txt (1.8 KB, 7 views)
  #2  
Old 28-Apr-2004, 09:01
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi kelly.

The problem you are having is that you are simply assigning your char* to a static string array (sequence). Therefore all of your seq_names are simply pointing to the exact same static memory location as defined by sequence. And the contents of sequence are getting overwritten by each call to fgets. Since "[1]" is the last entry in your file, this is the apparent contents of all of your seq_names.

To fix this, you need to allocate memory for you seq_names and then use the strcpy command to copy the static string contents to the new memory location.

Try something like this:
CPP / C++ / C Code:
  
if(strstr(sequence,"*")!=NULL)
      {
      i++;
      /*Start of Change*/
      seq[i].seq_names = (char*) malloc(strlen(sequence)+1);
      strcpy(seq[i].seq_names,sequence);
      /*End of change*/

      fprintf(outFile,"%s\n",seq[i].seq_names);//first fprintf
      j=0;

Hope this helps,
d
  #3  
Old 28-Apr-2004, 21:33
kelly80 kelly80 is offline
New Member
 
Join Date: Apr 2004
Posts: 7
kelly80 is on a distinguished road
thanks dsmith,

yup, its really solve my problem ..em..but another problem comes,as u taught, i also change the part after

CPP / C++ / C Code:
      char *context[893];

      else if(strstr(sequence,"#")!=NULL){

      seq[i].context[j]=sequence;
                 j++;}
  
    }

to

CPP / C++ / C Code:
    char *context[10];   

    else if(strstr(sequence,"#")!=NULL){

    seq[i].context[j]=(char*) malloc(strlen(sequence)+1);
    strcpy(seq[i].context[j],sequence);
	
    fprintf(outFile,"%d",j);
    fprintf(outFile,"%s\n",seq[i].context[j]);   //first fprintf
    j++;}
	
    }

   for(int k=0;k<=i;k++)
           for(int l=0;l<j;l++){
	fprintf(outFile,"%s",seq[k].seq_names );
	fprintf(outFile,"%d",l);
	fprintf(outFile,"%s\n",seq[i].context[j]);   //2nd fprintf
}


but the program also cannot return me the correct answer in the 2nd fprintf..(it also return only [1][1]..) can u tell me what should i do??thanks again..
  #4  
Old 28-Apr-2004, 22:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hey kelly. I am not sure that I understand your subscripting. I may be wrong, but shouldn't this line:

CPP / C++ / C Code:
fprintf(outFile,"%s\n",seq[i].context[j]);

Be more like:
CPP / C++ / C Code:
fprintf(outFile,"%s\n",seq[k].context[l]);   

Otherwise you are just printing the last element each time.

HTH,
d
  #5  
Old 28-Apr-2004, 23:05
kelly80 kelly80 is offline
New Member
 
Join Date: Apr 2004
Posts: 7
kelly80 is on a distinguished road
thanks dsmith,
actually i also just realise that..see how careless i am..
  #6  
Old 29-Apr-2004, 03:24
kelly80 kelly80 is offline
New Member
 
Join Date: Apr 2004
Posts: 7
kelly80 is on a distinguished road
how to free the memory for these:
CPP / C++ / C Code:
seq[i].seq_names = (char*) malloc(strlen(sequence)+1);
seq[i].context[j]=(char*) malloc(strlen(sequence)+1);

thanks..
  #7  
Old 29-Apr-2004, 08:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by kelly80
how to free the memory for these:
CPP / C++ / C Code:
seq[i].seq_names = (char*) malloc(strlen(sequence)+1);
seq[i].context[j]=(char*) malloc(strlen(sequence)+1);

thanks..

The opposite of malloc is the free command. So you would use:

CPP / C++ / C Code:
free(seq[i].seq_names);

Cheers,
d
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent ! Pls Help Me ! mycashmoney C Programming Language 4 01-Jul-2006 23:49
Project amir_b C Programming Language 18 04-May-2004 23:40
reading a char* into struct data spike666 C Programming Language 7 19-Apr-2004 13:06
Help with struct IORB cIdiot C Programming Language 1 19-Apr-2004 09:09
struct fj8283888 C Programming Language 2 15-Apr-2004 13:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.