Problem in getting the starting and ending of a particular record from a file.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem in getting the starting and ending of a particular record from a file.
I have a program which will write informations like gid,gname,resname,restype,etc... in a file.I wrote a function to get the starting and ending position of a particular group.Stating of a particular group (say group 12)is denoted in a file by #gid12 and the end of the group is denoted by @gid12.I don't know wat mistake i have done. The code works correctly only for the 1st group in the file. when i try to get other groups information i am getting wrong output.Plz tel me wat is the bug in my program.And is there any other way to find the starting and ending position of a particular group from my file.
Does your output file look correct, or is it a goal that you are trying to achieve? You don't seem sure because you used the word "will".
You can verify the contents of your output file using the command 'cat filename'.
If your file is correct, and you want to find a string within the file and reports its position, then a combination of ftell() and fgets(), and of course feof(), should to the trick.
Code:
int FindStr(FILE* fp, const char* str)
{
// assume fp is a valid file pointer
// assume str is the string being sought
int pos = -1;
while (!feof(fp))
{
char line[256] = {0}; // 256 was arbitrarily chosen
int curLoc = ftell(fp);
fgets(line, sizeof(line), fp);
if (strncmp(line, str, strlen(str) == 0)
{
pos = curLoc;
break;
}
}
return pos;
}
Last edited by dwhitney67; 04-02-2009 at 07:59 AM.
Thanx for ur reply dude.But i tried the function which u have posted.But it returns -1 for all the strings.i.e, its not finding the specified string in the file.
Thanx for ur reply dude.But i tried the function which u have posted.But it returns -1 for all the strings.i.e, its not finding the specified string in the file.
The FindStr() does not work because it has a bug in it.
Sorry about that... replace the strncmp() line with:
Code:
if (strncmp(line, str, strlen(str)) == 0)
The original statement was missing a closing parenthesis for the strncmp() function call.
Last edited by dwhitney67; 04-02-2009 at 09:07 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.