# fscanf



## LoneWolf071 (Nov 10, 2004)

```
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *checknames;

char name[100][100];
int pullname, rndnmb, i, j, total;
char *filename = "checknames.txt";

	checknames = fopen(filename, "r+");
	
	if (checknames == NULL)
	{
	printf("Error Opening the file\n");
	exit(1);
	}
 		for (i = 0, j = 0; fscanf(checknames, "%c", name[j][i]) != EOF; i++)
		{
			if (name[j][i] == '\n' || name[j][i] == '\0')
			{
				j++;
			}
		}
		total = j;
		
		for (j = 0; j < total; j++)
		{
			printf("%s\n", name[j]);
		}

return 0;
}
```
ok... this is only the base, so don't comment on libraries and that, but i'm trying to make a program that will scan names from a file and print them... and my computer crashes when i run it, anyone got any ideas?


----------



## elf (Jul 19, 2002)

I can tell you what line is your problem, but am currently still looking for a solution...


```
for (i = 0, j = 0; fscanf(checknames, "%c", name[j][i]) != EOF; i++)
```
is where you are getting the problems. Fix that and you will fix it.

edit: would fgets work? I know you could do it with that, and since you can't have more than 100 names it should be fairly easy to do. 

something like

```
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *checknames;

char name[100][100];
int pullname, rndnmb, i, j=0, total;
char *filename = "checknames.txt";
        checknames = fopen(filename, "r");
        if (checknames == NULL)
        {
        printf("Error Opening the file\n");
        exit(1);
        }
                for (i = 0; fgets(name[i], 100, checknames) != 0; i++)
                total = i;
                for (j = 0; j <= total; j++)
                {
                        printf("%s\n", name[j]);
                }
fclose(checknames);
return 0;
}
```
then in the checknames.txt you just put each name on a different line. It will read till it hits the \n


----------



## LoneWolf071 (Nov 10, 2004)

i forgot to post that i fixed it... the i variable was getting set to 1, so the second name is put at name[1][1] instead of name[1][0]... i just set it to set i at -1; but now i have a new problem, i'll post it later...


```
/*  Written By : Sisyphus
	Purpose : Take strings from a file and pick one out at random 
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int rndsel();
	 
int main()
{
	char choice = 'a'; 

		while (choice != 'x')
		{

			printf("Add To List(A), Delete List(D), or Random Select(R) Or Exit(X)\n");
			scanf("%c", &choice);

			

			if (choice >= 'A' && choice <= 'Z')
			choice += 32;
		
			//printf("You Chose %d %c\n", choice, choice);// --- use this if the choice-if loop gives you trouble 
			
			switch(choice)
			{
				case 'r':
				rndsel();
				break;
				
				case 'x':
				break;
				
				default:
				printf("You Didn't Enter A Proper Choice\n");
				continue;
			}	
		}
	return 0;
}

int rndsel()
{
	
FILE *checknames;
time_t now;
char name[1000][1000];
int pullname, rndnmb, i, j, total;
char filename[80], choice;

printf("Enter The Name Of The File You Want : ");
scanf("%s", filename);

time(&now);
rndnmb = rand();
rndnmb *= now;


	checknames = fopen(filename, "r");
	
		if (checknames == NULL)
		{
			printf("Error Opening the file\n");
			exit(1);
		}
 		
		for (i = 0, j = 0; fscanf(checknames, "%c", &name[j][i]) != EOF; i++)
		{
			if (name[j][i] == '\n')
			{		
				name[j][i] -= '\n';
				i = -1;
				j++;
			}
		}
		
		total = j + 1;
		rndnmb %= total;
		
		if (rndnmb < 0)
		{
			rndnmb = -(rndnmb);
		}

			
printf("%s\n", name[rndnmb]);

fclose(checknames);
return 0;
}
```
at first run, it is ok, but once it has run, coming back to the menu, it pops it up twice. for some reason it assign choice 10, then let's the user define it... but i don't want it to do that... why is it doing that?


----------



## elf (Jul 19, 2002)

I'm not sure exactly why, when you scanf it is leaving something in the buffer, just include a fflush

something like 

```
while (choice != 'x')
		{
                        [b]fflush(stdin);[/b] //flushes the stdin buffer
			printf("Add To List(A), Delete List(D), or Random Select(R) Or Exit(X)\n");
			scanf("%c", &choice);
```


----------



## LoneWolf071 (Nov 10, 2004)

Didn't work... i have no idea why?


----------



## elf (Jul 19, 2002)

let me log back in to my compiler and I'll post the full code


```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
                                                                                
int rndsel();
                                                                                
int main()
{
        char choice = 'a';
                                                                                
                while (choice != 'x')
                {
                        fflush(stdin);
                        printf("Add To List(A), Delete List(D), or Random Select(R) Or Exit(X)\n");
                        scanf("%c", &choice);
                                                                                
                                                                                
                                                                                
                        if (choice >= 'A' && choice <= 'Z')
                        choice += 32;
                                                                                
                        //printf("You Chose %d %c\n", choice, choice);// --- use this if the choice-if loop gives you trouble
                                                                                
                        switch(choice)
                        {
                                case 'r':
                                rndsel();
                                break;
                                                                                
                                case 'x':
                                break;
                                                                                
                                default:
                                printf("You Didn't Enter A Proper Choice\n");
                                continue;
                        }
                }
        return 0;
}
                                                                                
int rndsel()
{
 
FILE *checknames;
time_t now;
char name[1000][1000];
int pullname, rndnmb, i, j, total;
char filename[80], choice;
 
printf("Enter The Name Of The File You Want : ");
scanf("%s", filename);
 
time(&now);
rndnmb = rand();
rndnmb *= now;
 
 
        checknames = fopen(filename, "r");
        
                if (checknames == NULL)
                {
                        printf("Error Opening the file\n");
                        exit(1);
                }
        
                for (i = 0, j = 0; fscanf(checknames, "%c", &name[j][i]) != EOF; i++)
                {
                        if (name[j][i] == '\n')
                        {
                                name[j][i] -= '\n';
                                i = -1;
                                j++;
                        }
                }
        
                total = j + 1;
                rndnmb %= total;
        
                if (rndnmb < 0)
                {
                        rndnmb = -(rndnmb);
                }
        
        
printf("%s\n", name[rndnmb]);
        
fclose(checknames);
return 0;
}
```
bah it wouldn't let me copy paste, and it takes forEVER to log in, but there it is. It compiles and runs and only prints menu once.


----------



## LoneWolf071 (Nov 10, 2004)

```
/* 
Written By : Sisyphus A.K.A. @)--}---
Purpose : Take strings from a file and pick one out at random 
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char rndsel();
	 
int main()
{
	char choice; 

	while (choice != 'x')
	{
		fflush(stdout);
		fflush(stdin);
		printf("Add To List(A), Delete List(D), or Random Select(R) Or Exit(X)\n");
		scanf("%c", &choice);
	
		fflush(stdout);
		fflush(stdin);
		if (choice >= 'A' && choice <= 'Z')
		choice += 32;
		
		// printf("You Chose %d %c\n", choice, choice);// --- use this if the choice-if loop gives you trouble 
			
		switch(choice)
		{
			case 'r':
			rndsel();
			fflush(stdout);
			fflush(stdin);
				
			case 'x':
			break;
				
			default:
			printf("You Didn't Enter A Proper Choice\n");
			continue;
		}	
		
			fflush(stdout);
			fflush(stdin);		
	}
	return 0;
}

char rndsel()
{
	
FILE *checknames;
time_t now;
char name[10000][1000];
int rndnmb, i, j, total;
char filename[80], choice;

printf("Enter The Name Of The File You Want : ");
scanf("%s", filename);

time(&now);
rndnmb = rand();
rndnmb *= now;


	checknames = fopen(filename, "r");
	
		if (checknames == NULL)
		{
			printf("Error Opening the file\n");
			exit(1);
		}
 		
		for (i = 0, j = 0; fscanf(checknames, "%c", &name[j][i]) != EOF; i++)
		{
			if (name[j][i] == '\n')
			{		
				name[j][i] -= '\n';
				i = -1;
				j++;
			}
		}
		
		total = j + 1;
		rndnmb %= total;
		
		if (rndnmb < 0)
		{
			rndnmb = -(rndnmb);
		}

		for ( ; ;)
		{

			if (name[rndnmb] == '\n' || name[rndnmb] == NULL)
			{
				rndnmb = rand();
				rndnmb *= now;
			}
			
			else
			{
			break;
			}
		}	 
	

printf("\n\nThe Random Choice Is : %s\n\n", name[rndnmb]);

fclose(checknames);
choice = 'r';
return choice;
}
```
here it is, i flushed everything and it still doesn't work.... Arg... i hate programming ..


----------



## elf (Jul 19, 2002)

Are you recompiling? it works just fine on mine, I do all the choices (except x) and then it will display the menu, just once. You don't need to clear stdout, and you only need to clear stdin before you scanf (because scanf doesn't clear \n out of the buffer)


----------



## johnwill (Sep 26, 2002)

FWIW, I don't use fscanf, but rather read a string in and parse it with sscanf.


----------



## elf (Jul 19, 2002)

I prefer fgets, but to each his own...


----------



## LoneWolf071 (Nov 10, 2004)

NM, i got it...


----------



## elf (Jul 19, 2002)

What was wrong?


----------



## LoneWolf071 (Nov 10, 2004)

I though that it didn't matter where you placed the fflush(stdin), i normally placed it above at the top of the loop, but when places directly above the scanf function, it did it...


----------



## elf (Jul 19, 2002)

:wink: you have to clear the buffer each time around to clear out the \n so technically you should stick it after the scanf...


----------



## LoneWolf071 (Nov 10, 2004)

wait... now it won't do it, when i run it, it still gives me that. i do the fflush(stdin) right before ther scanf(), after the menu ... *crying* it works... it won't work, i hate these things, *throwing computer out of the windows...*


----------



## johnwill (Sep 26, 2002)

elf said:


> I prefer fgets, but to each his own...


I use fgets() to grab the string, then sscanf() to parse it. Prevents program hangs and weird happenenings, you have better control over the process. :smile:


----------



## LoneWolf071 (Nov 10, 2004)

ok... the book i learned from taught neither... so can you post on how to use them please?

ok here is the final source, almost complete... it works on windows under the borland compiler, but for some reason, under gcc(linux), it does the repeating menu...


```
/*  Written By : Sisyphus
	Purpose : Take strings from a file and pick one out at random 
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void  rndsel(void);
void  addsel(void);
int main()
{
	char choice = 'a'; 

		while (choice != 'x')
		{

			printf("Add To List(A), Delete List(D), or Random Select(R) Or Exit(X)\n");
			fflush(stdin);
			scanf("%c", &choice);

			

			if (choice >= 'A' && choice <= 'Z')
			choice += 32;
		
			//printf("You Chose %d %c\n", choice, choice);// --- use this if the choice-if loop gives you trouble 
			
			switch(choice)
			{
				case 'r':
				rndsel();
				break;
				
				case 'a':
				addsel();
				break;
				
				case 'x':
				break;
				
				default:
				printf("You Didn't Enter A Proper Choice\n");
				continue;
			}	
		}
	return 0;
}

void rndsel()
{
	
FILE *checknames;
time_t now;
char name[1000][1000];
int pullname, rndnmb, i, j, total;
char filename[80], choice;

printf("Enter The Name Of The File You Want : ");
scanf("%s", filename);

time(&now);
rndnmb = rand();
rndnmb *= now;


	checknames = fopen(filename, "r");
	
		if (checknames == NULL)
		{
			printf("Error Opening the file\n");
			exit(1);
		}
 		
		for (i = 0, j = 0; fscanf(checknames, "%c", &name[j][i]) != EOF; i++)
		{
			if (name[j][i] == '\n')
			{		
				name[j][i] -= '\n';
				i = -1;
				j++;
			}
		}
		
		total = j + 1;
		rndnmb %= total;

		
		for ( ; ;)
		{
			if (*name[rndnmb] == '\n' || *name[rndnmb] == NULL)
			{
				
				rndnmb = rand();
				rndnmb *= now;
				rndnmb %= total;
			}
			else 
			{
				break;
			}		
		}
				
		if (rndnmb < 0)
		{
			rndnmb = -(rndnmb);
		}


			
printf("\n\nYou Got : %s\n\n\n", name[rndnmb]);

fclose(checknames);
}

void addsel()
{
	FILE *opend;
	char opadfi[80], addnames[80];
//	int i, j;

	printf("Enter The File You wish to add to : ");
	scanf("%s", opadfi);	
	
	opend = fopen(opadfi, "a+");
	
	if (opend == NULL)
	{
		printf("Error Opening the file\n");
		exit(1);
	}
	
	for (; ; )
	{
		printf("What String Would You Like To Add To The File?(Only One At A Time Or X to Exit)\n");
		scanf("%s", addnames);
		if (*addnames == 'x' || *addnames == 'X')
		{
			break;
		}
		fprintf(opend, "%s\n", addnames);
	}
}
```


----------

