473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Does C distinguish between a C source file and a text file?

Hi guys!I am trying to write a program which will segregate some
selected keywords from a given file.The source code is given alongwith

#include<stdio.h>
#include<string.h>

char key_set[][4]={"ami\0","inc\0","lud\0"};
#define MAX_KW_SIZE 10
#define MAX_OCUR 100

struct kbuf_node{
char keyword[MAX_KW_SIZE];
int byte_offset[MAX_OCUR];

}buf_node;
int main(int argc,char *argv[])
{
FILE *fp,*fp_kbuf;
char *s;
int i,offset=0,cnt,FLAG=0;

fp_kbuf=fopen("buf.txt","wb"); //the file in which i will save the
keywords and their byte offsets
for(i=0;i<=2;i++)
{
cnt=0;
offset=0;
s=(char*)malloc(sizeof(key_set[i]));
fp=fopen(argv[0],"rb"); //the master file given as argument

if(fp==NULL)
{
printf("Unable to open file");
exit(0);
}
while(fgets(s,sizeof(key_set[i]),fp)!=NULL)
{
//printf("Before setpos%u\n",ftell(fp));
offset++;
if(strcmp(s,key_set[i])==0)
{
//printf("%s\t %d\n",s,offset);
if(FLAG==0)
{
strcpy(buf_node.keyword,s);
FLAG=1;
}
buf_node.byte_offset[cnt++]=offset;

}
fsetpos(fp,&offset);
//printf("After setpos %u\n",fp);
}

//write to the kw-buffer file
fwrite(&buf_node,sizeof(buf_node),1,fp_kbuf);
FLAG=0;

fclose(fp);
}
fclose(fp_kbuf);
fp_kbuf=fopen("buf.txt","rb");
while(fread(&buf_node,sizeof(buf_node),1,fp_kbuf)> 0)
{
printf("\n%s\t",buf_node.keyword);
for(i=0;i<cnt;i++)
printf("%d\t",buf_node.byte_offset[i]);
printf("\n");
}

return 0;
}
After compiling the file when i run the program using a C source file
as my file then it works fine.BUt with a text file it only reports the
first occurence of the keywords!I am using gcc under FC5

Aug 10 '06 #1
4 1571
amit wrote:
Hi guys!I am trying to write a program which will segregate some
selected keywords from a given file.The source code is given alongwith

#include<stdio.h>
#include<string.h>
Spaces are given away free with cornflakes so you don't need to save
them For better readability try
#include <stdio.h>
#include <string.h>
char key_set[][4]={"ami\0","inc\0","lud\0"};
You don't need to specify the \0. Also, as this is a fixed list and you
don't want to modify the data it would be better to simply use:
const char *key_set[]={"ami","inc","lud"};

#define MAX_KW_SIZE 10
#define MAX_OCUR 100

struct kbuf_node{
char keyword[MAX_KW_SIZE];
int byte_offset[MAX_OCUR];

}buf_node;
Using globals is generally a bad idea.
int main(int argc,char *argv[])
{
FILE *fp,*fp_kbuf;
char *s;
int i,offset=0,cnt,FLAG=0;
Traditionally all upper case names are only used for macros and
enumerations.
fp_kbuf=fopen("buf.txt","wb"); //the file in which i will save the
keywords and their byte offsets
Please don't use // style comments on Usenet. They don't survive line
wrapping as you can see. Also they are not supported by C89 the most
commonly implemented C standard.
for(i=0;i<=2;i++)
Magic numbers are bad. They make maintenance harder.
for(i=0; i < (sizeof key_set / sizeof key_set[0]); i++)
{
cnt=0;
offset=0;
s=(char*)malloc(sizeof(key_set[i]));
Don't cast the return value of malloc it isn't required. The reason the
compiler complained at you without the cast is because you failed to
include stdlib.h. This is a serious error and causes the code to fail on
some modern platforms.

Also you never free the memory so each time round the loop you leak memory.

In any case, since this is a constant size why did you not just allocate
an array of the appropriate size?
fp=fopen(argv[0],"rb"); //the master file given as argument
argv[0] is the name of the executable, or some variation of it, are you
sure your didn't mean argv[1] after checking that argc is at least 2?
if(fp==NULL)
{
printf("Unable to open file");
exit(0);
Better would be to signal an error rather than success!
exit(EXIT_FAILURE);
}
while(fgets(s,sizeof(key_set[i]),fp)!=NULL)
{
//printf("Before setpos%u\n",ftell(fp));
offset++;
if(strcmp(s,key_set[i])==0)
{
//printf("%s\t %d\n",s,offset);
if(FLAG==0)
{
strcpy(buf_node.keyword,s);
FLAG=1;
}
buf_node.byte_offset[cnt++]=offset;

}
fsetpos(fp,&offset);
The second argument to fsetpos is a pointer to fpos_t NOT a pointer to
int. On some implementations fpos_t could be a long or a struct and thus
severely break your code.
//printf("After setpos %u\n",fp);
}

//write to the kw-buffer file
fwrite(&buf_node,sizeof(buf_node),1,fp_kbuf);
FLAG=0;

fclose(fp);
}
fclose(fp_kbuf);
fp_kbuf=fopen("buf.txt","rb");
while(fread(&buf_node,sizeof(buf_node),1,fp_kbuf)> 0)
{
printf("\n%s\t",buf_node.keyword);
for(i=0;i<cnt;i++)
printf("%d\t",buf_node.byte_offset[i]);
printf("\n");
}

return 0;
}
After compiling the file when i run the program using a C source file
as my file then it works fine.BUt with a text file it only reports the
first occurence of the keywords!I am using gcc under FC5
Try turning up the warnings and actually fixing the things it complains
about. Start with considering all the bits I have pointed out that are
wrong. Although you approach does not seem very efficient to me anyway.
--
Flash Gordon
Still sigless on this computer.
Aug 10 '06 #2
hi Flash!
If u cud suggest a more efficient way of doing this?I am really sory
for the goof-ups!I am new to usenet!plz excuse me!

Flash Gordon wrote:
amit wrote:
Hi guys!I am trying to write a program which will segregate some
selected keywords from a given file.The source code is given alongwith

#include<stdio.h>
#include<string.h>

Spaces are given away free with cornflakes so you don't need to save
them For better readability try
#include <stdio.h>
#include <string.h>
char key_set[][4]={"ami\0","inc\0","lud\0"};

You don't need to specify the \0. Also, as this is a fixed list and you
don't want to modify the data it would be better to simply use:
const char *key_set[]={"ami","inc","lud"};

#define MAX_KW_SIZE 10
#define MAX_OCUR 100

struct kbuf_node{
char keyword[MAX_KW_SIZE];
int byte_offset[MAX_OCUR];

}buf_node;

Using globals is generally a bad idea.
int main(int argc,char *argv[])
{
FILE *fp,*fp_kbuf;
char *s;
int i,offset=0,cnt,FLAG=0;

Traditionally all upper case names are only used for macros and
enumerations.
fp_kbuf=fopen("buf.txt","wb"); //the file in which i will save the
keywords and their byte offsets

Please don't use // style comments on Usenet. They don't survive line
wrapping as you can see. Also they are not supported by C89 the most
commonly implemented C standard.
for(i=0;i<=2;i++)

Magic numbers are bad. They make maintenance harder.
for(i=0; i < (sizeof key_set / sizeof key_set[0]); i++)
{
cnt=0;
offset=0;
s=(char*)malloc(sizeof(key_set[i]));

Don't cast the return value of malloc it isn't required. The reason the
compiler complained at you without the cast is because you failed to
include stdlib.h. This is a serious error and causes the code to fail on
some modern platforms.

Also you never free the memory so each time round the loop you leak memory.

In any case, since this is a constant size why did you not just allocate
an array of the appropriate size?
fp=fopen(argv[0],"rb"); //the master file given as argument

argv[0] is the name of the executable, or some variation of it, are you
sure your didn't mean argv[1] after checking that argc is at least 2?
if(fp==NULL)
{
printf("Unable to open file");
exit(0);

Better would be to signal an error rather than success!
exit(EXIT_FAILURE);
}
while(fgets(s,sizeof(key_set[i]),fp)!=NULL)
{
//printf("Before setpos%u\n",ftell(fp));
offset++;
if(strcmp(s,key_set[i])==0)
{
//printf("%s\t %d\n",s,offset);
if(FLAG==0)
{
strcpy(buf_node.keyword,s);
FLAG=1;
}
buf_node.byte_offset[cnt++]=offset;

}
fsetpos(fp,&offset);

The second argument to fsetpos is a pointer to fpos_t NOT a pointer to
int. On some implementations fpos_t could be a long or a struct and thus
severely break your code.
//printf("After setpos %u\n",fp);
}

//write to the kw-buffer file
fwrite(&buf_node,sizeof(buf_node),1,fp_kbuf);
FLAG=0;

fclose(fp);
}
fclose(fp_kbuf);
fp_kbuf=fopen("buf.txt","rb");
while(fread(&buf_node,sizeof(buf_node),1,fp_kbuf)> 0)
{
printf("\n%s\t",buf_node.keyword);
for(i=0;i<cnt;i++)
printf("%d\t",buf_node.byte_offset[i]);
printf("\n");
}

return 0;
}
After compiling the file when i run the program using a C source file
as my file then it works fine.BUt with a text file it only reports the
first occurence of the keywords!I am using gcc under FC5

Try turning up the warnings and actually fixing the things it complains
about. Start with considering all the bits I have pointed out that are
wrong. Although you approach does not seem very efficient to me anyway.
--
Flash Gordon
Still sigless on this computer.
Aug 11 '06 #3
On 11 Aug 2006 09:48:45 -0700, "amit" <am*********@gmail.comwrote:
>If u cud suggest a more efficient way of doing this?I am really sory
for the goof-ups!I am new to usenet!plz excuse me!

Flash Gordon wrote:
>amit wrote:
Hi guys!I am trying to write a program which will segregate some
You can start by actually reading and understanding what Flash Gordon
wrote. Next, when you come back with more questions, take care to
intersperse your remarks after the text they refer to, rather than
top-posting. Also, stop the baby talk. Use "you", "could", and
"please" instead of "u", "cud", and "plz."

--
Al Balmer
Sun City, AZ
Aug 11 '06 #4
"amit" <am*********@gmail.comwrites:
hi Flash!
If u cud suggest a more efficient way of doing this?I am really sory
for the goof-ups!I am new to usenet!plz excuse me!
[...]

Don't top-post. Your response goes *below* any quoted text you're
replying to, or interspersed with it. See most of the articles in
this newsgroup for examples. See also
<http://www.caliburn.nl/topposting.html>.

Don't use silly abbreviations. Take the time to spell out words, and
add a space or two after each sentence. For example:

If you could suggest a more efficient way of doing this? I am
really sorry for the goof-ups. I am new to Usenet. Please excuse
me.

It makes it *much* easier to read.

Finally, Flash already gave you some very good advice in the article
you replied to. Try following it before you ask the same question
again.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: JDS | last post by:
The following is not working for me. What I want to do is this- my cgiparms have a variable Test_1 or Test_2 .... generated depending on what is chosen on a form. I want to check the value of...
2
by: greatbooksclassics | last post by:
Open Source DRM? What does everyone think about it? Will Open Source DRM ever catch up to MS DRM? Will DRM ever be integrated into common LAMP applications?...
1
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not...
5
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
1
by: Luis Esteban Valencia | last post by:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source...
1
by: sal | last post by:
Greets, All Question with udl and connection string does it work? I'm having problems with the code below I keep getting an error "Keyword not supported 'File Name'" I was just following...
15
by: amit.man | last post by:
Hi, i have newbie qestion, when i write #include <somthing.h> the precompiler subtitue that line with the lines from "somthing.h" header file. when, where and how the compiler insert the...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.