Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 03-04-2008, 11:55 PM   #1
Manju Lata Joshi
LQ Newbie
Registered: Feb 2008
Posts: 1

Rep: Reputation: 0
Angry 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:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.ain()
int i,j,x;
FILE *fp1, *fp2;

char c, pos[10], word[50];
fp1 = fopen("new.txt", "r");
fp2 = fopen("masterlistnoun", "w");
{ printf("File cannot open\n ");
c = getc(fp1);

/* Check for noun tags */
{ i=0;
{ word[i++]=c;
{ pos[j++]=c;
if(strcmp(pos, "NNPS")==0 || strcmp(pos, "NN")==0 || strcmp(pos, "NNS")==0 || strcmp(pos, "NNP")==0)
printf(fp2, "%s", word);
putc(' ' ,fp2);
}//outer loop
return 0;


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.

Old 03-05-2008, 12:57 AM   #2
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Is this just a function to a larger program otherwise i would ask where the main is ?

c = getc(fp1); //Why is this being done ?

/* Check for noun tags */
while((c=getc(fp1))!=EOF) //the above is overwritten here ?
{ i=0;
{ word[i++]=c;
Old 03-05-2008, 01:04 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Well, this 'DT university' is >10 chars long, so you're writing off the end of pos. (I think)
Old 03-05-2008, 09:08 AM   #4
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
The first bug that leaps out as I look at the code:

Originally Posted by Manju Lata Joshi View Post
                {       pos[j++]=c;
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?

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.

Last edited by johnsfine; 03-05-2008 at 09:18 AM.
Old 03-05-2008, 09:24 AM   #5
Senior Member
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in assigning real time scheduling to pthread in linux jasdeep_js Linux - Kernel 1 09-07-2008 02:44 AM
Something Loopy in Assigning Strings MattFunke Programming 3 09-19-2007 03:26 AM
Problem assigning new user to my group Odyssey1942 Linux - Newbie 30 08-24-2006 03:39 PM
Assigning Static IP Problem canuck_barlow Linux - Newbie 5 01-06-2006 03:54 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration