I have an assignment that calls for me to only use standard C. I am currently trying to take in input from a file using fopen() and then take in lines from the file using fgets(). The original problem was that fgets() quits taking in input after six lines. "Something" changed. Now it quits taking in input after four lines. I was wondering if someone could take a look at the code below and see if any solution comes to mind. Thanks in advance. The input for the program is as follows:
This is the file "params.txt"

infile=hbglobin.txt
left=GAAGTTCCTATWY
spacer=TTTCTAGA
csvfile=hbglobin_FRT-likes.csv
threshold=80
symmetry=yes
doublestranded=no
outfile=hbglobin_FRT-likes.txt
right=RWATAGGAACTTC

/*
 * This is the Get Parameters Function. The goal of this function 
 * is take in parameters from a text file for the program to use.
 * */
void getParam(char* paramfile)
{
	printf("Getting Parameters NOW\n");	// A Print statement for debugging purposes
	extern char *filein,*l,*s,*r,*ds,*sym,*fileout,*csvfileout,*sym,*ds;
	extern int threshold;
	int index;
	char line[200];  
	char *name;
	char *value;
	char *tempname;
	char *tempvalue;	
	tempvalue=(char*)malloc(sizeof(char));	
	tempname=(char*)malloc(sizeof(char));	
	name=(char*)malloc(sizeof(char));
	value=(char*)malloc(sizeof(char));	
	
	FILE *paramsource;
	
	paramsource = fopen("params.txt", "r"); 
	
	
	if(paramsource==NULL) 
	{
		printf("Error: can't open file.\n");
		/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
	}
	else 
	{
		printf("File opened successfully\n");
		while(fgets(line, max_line+1,paramsource)!=NULL) 
		{ 
			printf("Beginning\n");//Print Statement for debugging purposes			
			index=indexofE(line);//This function call locates the index of the string where an equal sign is
			
			strncpy(name,line,(index));//Creating substring "name" ( the left side of the "=" sign)
			strncpy(value,line+(index+1),(strlen(line)));//Creating substring "value" (the right side of the "=" sign)
							
			printf("Name is %s Value is %s\n",name,value);//Print statement for debugging purposes	
				if(strcmp(name,"infile")==0)
					{			
						
					filein=malloc(sizeof(char));						
					strcpy(filein,value);
											
					}
				else if(strcmp(name,"left")==0)		
					{	
						strcpy(l,value); 
						
					}
				else if(strcmp(name,"spacer")==0) 
					{	
						strcpy(s,value);
						
					}
				else if(strcmp(name,"right")==0) 
					{
						strcpy(r,value);
						
					}
				else if(strcmp(name,"threshold")==0) 
					{	
						threshold=atoi(value);
					}
				else if(strcmp(name,"doublestranded")==0) 
					{	
						strcpy(ds,value);
					}
								
				else if(strcmp(name,"symmetry")==0)
					{	
						strcpy(sym,value);
															
					}
				else if(strcmp(name,"outfile")==0)
					{	
						 						
						fileout=(char*)malloc(strlen(value)*sizeof(char));					   
						strcpy(fileout,value);
											
					}
				else if(strcmp(name,"csvfile")==0) 
					{
					   
					   printf("Inside csvfile if statement\n");
					   csvfileout=(char*)malloc(strlen(value)*sizeof(char));
					   strcpy(csvfileout,value); 
					   					
					}
				
								
					/*These are so name and value  start fresh everytime and 
					 * there are no remnants left over from the previous strings.
					 * */
				memset(name,0,strlen(name));
				memset(value,0,strlen(value));			
									
				}
			
			printf("Left the while loop\n");//The print statement for debugging purposes
    			
    			if ( strcmp(ds,"yes\n")==0||strcmp(ds,"Yes\n")==0||strcmp(ds,"yes")==0||strcmp(ds,"Yes")==0 ) 
				{
				doublestranded = 1;
    			}
			else if ( strcmp(ds,"no\n")==0||strcmp(ds,"No\n")==0||strcmp(ds,"no")==0||strcmp(ds,"No")==0 )
				{
				doublestranded = 0;
    			}
			else 
				{
				printf("doublestranded doesn't look right...\n");
    			}
				
			if (strcmp(sym,"yes\n")==0||strcmp(sym,"Yes\n")==0||strcmp(sym,"yes")==0||strcmp(sym,"Yes")==0) 
				{
				symmetry = 1;
    			}
			else if (strcmp(sym,"no\n")==0||strcmp(sym,"No\n")==0||strcmp(sym,"no")==0||strcmp(sym,"No")==0)
			{
								
				symmetry = 0;
			}
			else
				{
				printf("symmetry doesn't look right...\n");
    			}
				printf("read parameters from %s\n",paramfile);  
				params = 1;
		
		}	
	}

I have an assignment that calls for me to only use standard C. I am currently trying to take in input from a file using fopen() and then take in lines from the file using fgets(). The original problem was that fgets() quits taking in input after six lines. "Something" changed. Now it quits taking in input after four lines. I was wondering if someone could take a look at the code below and see if any solution comes to mind. Thanks in advance. The input for the program is as follows:
This is the file "params.txt"

infile=hbglobin.txt
left=GAAGTTCCTATWY
spacer=TTTCTAGA
csvfile=hbglobin_FRT-likes.csv
threshold=80
symmetry=yes
doublestranded=no
outfile=hbglobin_FRT-likes.txt
right=RWATAGGAACTTC
/*
 * This is the Get Parameters Function. The goal of this function 
 * is take in parameters from a text file for the program to use.
 * */
void getParam(char* paramfile)
{
    printf("Getting Parameters NOW\n"); // A Print statement for debugging purposes
    extern char *filein,*l,*s,*r,*ds,*sym,*fileout,*csvfileout,*sym,*ds;
    extern int threshold;
    int index;
    char line[200];  
    char *name;
    char *value;
    char *tempname;
    char *tempvalue;    
    tempvalue=(char*)malloc(sizeof(char));  
    tempname=(char*)malloc(sizeof(char));   
    name=(char*)malloc(sizeof(char));
    value=(char*)malloc(sizeof(char));  

    FILE *paramsource;

    paramsource = fopen("params.txt", "r"); 


    if(paramsource==NULL) 
    {
        printf("Error: can't open file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    }
    else 
    {
        printf("File opened successfully\n");
        while(fgets(line, max_line+1,paramsource)!=NULL) 
        { 
            printf("Beginning\n");//Print Statement for debugging purposes          
            index=indexofE(line);//This function call locates the index of the string where an equal sign is

            strncpy(name,line,(index));//Creating substring "name" ( the left side of the "=" sign)
            strncpy(value,line+(index+1),(strlen(line)));//Creating substring "value" (the right side of the "=" sign)

            printf("Name is %s Value is %s\n",name,value);//Print statement for debugging purposes  
                if(strcmp(name,"infile")==0)
                    {           

                    filein=malloc(sizeof(char));                        
                    strcpy(filein,value);

                    }
                else if(strcmp(name,"left")==0)     
                    {   
                        strcpy(l,value); 

                    }
                else if(strcmp(name,"spacer")==0) 
                    {   
                        strcpy(s,value);

                    }
                else if(strcmp(name,"right")==0) 
                    {
                        strcpy(r,value);

                    }
                else if(strcmp(name,"threshold")==0) 
                    {   
                        threshold=atoi(value);
                    }
                else if(strcmp(name,"doublestranded")==0) 
                    {   
                        strcpy(ds,value);
                    }

                else if(strcmp(name,"symmetry")==0)
                    {   
                        strcpy(sym,value);

                    }
                else if(strcmp(name,"outfile")==0)
                    {   

                        fileout=(char*)malloc(strlen(value)*sizeof(char));                     
                        strcpy(fileout,value);

                    }
                else if(strcmp(name,"csvfile")==0) 
                    {

                       printf("Inside csvfile if statement\n");
                       csvfileout=(char*)malloc(strlen(value)*sizeof(char));
                       strcpy(csvfileout,value); 

                    }


                    /*These are so name and value  start fresh everytime and 
                     * there are no remnants left over from the previous strings.
                     * */
                memset(name,0,strlen(name));
                memset(value,0,strlen(value));          

                }

            printf("Left the while loop\n");//The print statement for debugging purposes

                if ( strcmp(ds,"yes\n")==0||strcmp(ds,"Yes\n")==0||strcmp(ds,"yes")==0||strcmp(ds,"Yes")==0 ) 
                {
                doublestranded = 1;
                }
            else if ( strcmp(ds,"no\n")==0||strcmp(ds,"No\n")==0||strcmp(ds,"no")==0||strcmp(ds,"No")==0 )
                {
                doublestranded = 0;
                }
            else 
                {
                printf("doublestranded doesn't look right...\n");
                }

            if (strcmp(sym,"yes\n")==0||strcmp(sym,"Yes\n")==0||strcmp(sym,"yes")==0||strcmp(sym,"Yes")==0) 
                {
                symmetry = 1;
                }
            else if (strcmp(sym,"no\n")==0||strcmp(sym,"No\n")==0||strcmp(sym,"no")==0||strcmp(sym,"No")==0)
            {

                symmetry = 0;
            }
            else
                {
                printf("symmetry doesn't look right...\n");
                }
                printf("read parameters from %s\n",paramfile);  
                params = 1;

        }   
    }

end quote.

dont now how much help this will be, i have a similar program that reads input from file and then does stuff with it
i used this fgets

while(fgets(line, sizeof line,file)

file being the name you named your file,
and just put a char array for line

char line[20];

i.e replace

while(fgets(line, max_line+1,paramsource)!=NULL)

with

while(fgets(line, sizeof line,paramsource)
commented: Yes indeed +19

Thanks for the try but it is still doing the same thing.

Ok, I just changed the params.txt input to this:

infile=hbglobin.txt
left=GAAGTTCCTATWY
right=RWATAGGAACTTC
spacer=TTTCTAGA
threshold=80
symmetry=yes
doublestranded=no
outfile=hbglobin_FRT-likes.txt
csvfile=hbglobin_FRT-likes.csv

So it seems that the program stops wherever csvfile is. Am I missing something obvious? It seems my if statements and the while loop should continue until the end of the files input. Any thoughts?

Lines 16-19: you allocate just one byte per each variable. Then you copy some substantial strings there. After that all bets are off. Fix the buffer overflows first, and then see if the problem persists.

commented: Bingo! +19

Thanks! That took care of everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.