problem in a 'C' program in assigning strings using Linux
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 a 'C' program in assigning strings using Linux
hi all,
As i am developing my project in 'C' language under Linux platform and GCC compiler. i have developed a small program to reat text from a file and copy words from the file into an array string and then after some comparision i wirte this string into another file. the program is compiled succesfullt but unable to run as it is giving error of SEGMENTATION FAULT all the time. i am not getting where the problem is in my program and how to solve it? i am sending my code to the forum as mentioned under:
my input file consist of following text as it is a simple text file:
I/PRP am/VBP Manju/NNP ./.
this/DT is/VBZ a/DT university/NN ./.
my output file is also a text file and totally blank:
***********************************************************************
i will be greatfull if someone solves or suggest somthing to solve the problem. my work is hanged for last three days.
The first bug that leaps out as I look at the code:
Quote:
Originally Posted by Manju Lata Joshi
Code:
while(c!=',')
{ pos[j++]=c;
c=getc(fp1);
}
The input you quoted has no ',' in it, so the above loop would read the rest of the file, then read EOF over and over again, until it has exceeded the allocated size of pos[] far enough to cause an access violation.
If there was a ',' and the outer loop worked, then the fact that the input file doesn't end exactly on a ',' could make the previous inner loop overwrite word[].
Also, are you taking newlines into account when reading the input?
Quote:
my output file is also a text file and totally blank:
File output is buffered, so even if there was a ',' in the input and the code went past the printf (which should have been fprintf) the access violation would still stop the buffered output from reaching the file.
you should start putting printf statements in your code. The text in the printf statements will appear on the console until the segfault occurs. This will let you narrow down where your problem is exactly.
edit: Also this question belongs in the programming forum, and I expect that the first mod to see it will move it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.