LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-04-2008, 10: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:

Code:
#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");
if(fp1==NULL)
{ printf("File cannot open\n ");
exit(1);
}
c = getc(fp1);

/* Check for noun tags */
while((c=getc(fp1))!=EOF)
{ i=0;
while(c!='/')
{ word[i++]=c;
c=getc(fp1);
}
word[i]='\0';
j=0;
c=getc(fp1);
while(c!=',')
{ pos[j++]=c;
c=getc(fp1);
}
pos[j]='\0';
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
fclose(fp1);
fclose(fp2);
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.

thanks,
Best
 
Old 03-04-2008, 11:57 PM   #2
exvor
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;
while(c!='/')
{ word[i++]=c;
c=getc(fp1);
}
 
Old 03-05-2008, 12:04 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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

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

Quote:
Originally Posted by Manju Lata Joshi View Post
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.

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

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


Reply



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 01:44 AM
Something Loopy in Assigning Strings MattFunke Programming 3 09-19-2007 02:26 AM
Problem assigning new user to my group Odyssey1942 Linux - Newbie 30 08-24-2006 02:39 PM
Assigning Static IP Problem canuck_barlow Linux - Newbie 5 01-06-2006 02:54 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:53 PM.

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