<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'how do you search a string from a file using file i/o?' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'how do you search a string from a file using file i/o?' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Fri, 24 May 2013 23:47:37 -0700</pubDate>
    <lastBuildDate>Fri, 24 May 2013 23:47:37 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>how do you search a string from a file using file i/o?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/427893/427893/how-do-you-search-a-string-from-a-file-using-file-io/</link>
      <description>this is what i have so far... i don't know how to begin my code for the "search" part. &lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
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",&amp;amp;a);
getchar();

switch(a){
case 1:
	in_file=fopen("input_corrected.csv", "r");
	if(in_file==NULL){
		return 1;
	}
	else{
		for (i=0; i&amp;lt;20; i++)
		{
			eee11[i] = get_student(in_file);
		}
	}
	check1=1;
	break;


case 2:
	if(check1!=1){
		printf("INVALID INPUT!");
		break;
	}
	for (i=0; i&amp;lt;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;
	}
}
}
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/427893/427893/how-do-you-search-a-string-from-a-file-using-file-io/</guid>
      <pubDate>Fri, 16 Mar 2012 20:10:54 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: how do you search a string from a file using file i/o?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/427893/427938/re-how-do-you-search-a-string-from-a-file-using-file-io/#427938</link>
      <description>Just equivalent code that is human readable.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
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",&amp;amp;a);

		switch(a){
		case 1:
			in_file=fopen("input_corrected.csv", "r");
			if(in_file==NULL)
				return 1;
			for (i=0; i&amp;lt;20; i++)
				eee11[i] = get_student(in_file);
			check1=1;
			break;
		case 2:
...
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Still, the code above don't make any sense, unless you are targeting a specific data handling.&lt;br /&gt;
From here on you have somewhat unlimited number of choices:&lt;br /&gt;
a. Read each time all the file line by line: fgets, strstr(to find delimiter)&lt;br /&gt;
b. Read all the file into memory&lt;br /&gt;
   - Store as is in a string&lt;br /&gt;
   - Cut into fields and manage two-dimensional array&lt;br /&gt;
   - Cut into fields and store array of structures&lt;br /&gt;
c. Employ Lex to cut delimiters&lt;br /&gt;
d. Employ ODBC with CSV driver&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
Notice that scanf is not good enough for production tools - you can't predict what garbage your user will put in.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/427893/427938/re-how-do-you-search-a-string-from-a-file-using-file-io/#427938</guid>
      <pubDate>Wed, 21 Mar 2012 03:37:49 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>