C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how do you search a string from a file using file i/o? Posted by confusedlilkid on 16 Mar 2012 at 8:10 PM
this is what i have so far... i don't know how to begin my code for the "search" part.

int main(void)
{
	int i;
	student_t eee11[20];
	int a=0;
	int check1=0;
	int check2=0;
	FILE *in_file;


while(a!=1||a!=2||a!=3||a!=4){
printf("\n\t[1] Load file\n\t[2] Compute individual average\n\t[3] Compute class average\n\t[4] Exit\n\t[5] Search for a Student\nSelect a number:");
scanf("%d",&a);
getchar();

switch(a){
case 1:
	in_file=fopen("input_corrected.csv", "r");
	if(in_file==NULL){
		return 1;
	}
	else{
		for (i=0; i<20; i++)
		{
			eee11[i] = get_student(in_file);
		}
	}
	check1=1;
	break;


case 2:
	if(check1!=1){
		printf("INVALID INPUT!");
		break;
	}
	for (i=0; i<20; i++)
	{
		eee11[i].average = get_ave(eee11[i]);
		printf("The average of %s is %0.2f.\n", eee11[i].std_no, eee11[i].average);
		printf("press enter to proceed...");
		getchar();
	}
	check2=1;
	break;


case 3:
	if(check1!=1){
		printf("INVALID INPUT!");
		break;
	}
	get_class_ave(eee11, 20);
	break;

case 4:
	return 0;
	fclose(in_file);

case 5:
    //insert search code here

default:
	printf("Invalid input!\n");
	a=0;
	}
}
}

Report
Re: how do you search a string from a file using file i/o? Posted by Arkadi on 21 Mar 2012 at 3:37 AM
Just equivalent code that is human readable.

int main(void)
{
	int i;
	student_t eee11[20];
	int a=0;
	int check1=0;
	int check2=0;
	FILE *in_file;

	while(true) {
		printf(
			"\n"
			"\t[1] Load file\n"
			"\t[2] Compute individual average\n"
			"\t[3] Compute class average\n"
			"\t[4] Exit\n"
			"\t[5] Search for a Student\n"
			"Select a number:");
		scanf("%d\n",&a);

		switch(a){
		case 1:
			in_file=fopen("input_corrected.csv", "r");
			if(in_file==NULL)
				return 1;
			for (i=0; i<20; i++)
				eee11[i] = get_student(in_file);
			check1=1;
			break;
		case 2:
...


Still, the code above don't make any sense, unless you are targeting a specific data handling.
From here on you have somewhat unlimited number of choices:
a. Read each time all the file line by line: fgets, strstr(to find delimiter)
b. Read all the file into memory
- Store as is in a string
- Cut into fields and manage two-dimensional array
- Cut into fields and store array of structures
c. Employ Lex to cut delimiters
d. Employ ODBC with CSV driver
...

Notice that scanf is not good enough for production tools - you can't predict what garbage your user will put in.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.