473,395 Members | 1,583 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.

when i compile this,. it is showing segmentation fault.. can anyone help me,..

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct intermediate
{
int addr;
char label[10];
char mnem[10];
char op[10];
}res;
struct symbol
{
char symbol[10];
int addr;
}sy;
int main()
{
FILE *s1,*p1;

s1=fopen("inter.txt","r+");
p1=fopen("symbol.txt","w");
while(!feof(s1))
{
fscanf(s1,"%d %s %s %s",&res.addr,res.label,res.mnem,res.op);
if(strcmp(res.label,"NULL")!=0)
{
strcpy(sy.symbol,res.label);
sy.addr=res.addr;
fprintf(p1,"%s\t%d\n",sy.symbol,sy.addr);
}
}
_fcloseall();

printf("symbol table created");
getch();
return;
}
Jul 15 '10 #1
3 1419
weaknessforcats
9,208 Expert Mod 8TB
Did you check s1 and p1 to be sure the files opened?
Jul 15 '10 #2
Oralloy
988 Expert 512MB
You should check for EOF or Error after the fscanf, as well.

(Actually, it's best to check for errors after all system calls, including your opens, but for disposable software you're usually fine.)

Likewise, the compare to the string "NULL" may not be what you want. Is that supposed to be your error check on the fscanf? If so, you'd better go back and read the documentation on fscanf once again.

fscanf documentation.
Jul 15 '10 #3
donbock
2,426 Expert 2GB
Segmentation fault is typically a run-time error, not a compile-time error. Are you sure about when this error occurs?

Please use CODE tags around your source code. That makes it easier to read the code, and provides line numbers that we can refer to.

You should follow Oralloy's advice and verify that each of your fopens succeed. You should also verify that fscanf and fprintf succeed. I'm not familiar with _fcloseall, can it fail?

On the fscanf line, you read in three strings, each into a 10-char array. What if the input file contains strings longer than that? Writing past the end of an array is undefined behavior. You want to protect your program from undefined behavior.

On the fscanf line, what happens if the input line doesn't match the format string? In that case some (or all) of the variables don't change. If that happens the first time through the loop then the variables contain total garbage, otherwise they contain values from the prevous iteration.

On the strcmp and strcpy lines, what happens if res.label contains garbage (see above)? Most notably, what if the array doesn't contain any nulls?
Jul 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Pud | last post by:
Hiya, I was wondering if anyone could help me, I am trying to create an example program that uses the xerces XML parser in Java. Unfortunately I have come across a problem when using this...
0
by: john.leonard.ireland | last post by:
Hi all, I don't know what I'm missing here, but if anyone could help me out I'd really appreciate it. Basically, everything is fine, compiles, installs, server runs, but the interactive mysql...
5
by: Ravikant | last post by:
Hello, Please check the following code .Let me know why its giving the segmentation fault while running it.First it asks to enter the name ,when entered it gives segmentation fault. But if I dont...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
20
by: ramu | last post by:
Hi, int main(void) { char ch='a'; printf("%s",ch); return 0; } Why does this give segmentation fault?
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
8
by: ChristophK | last post by:
Hallo, this is my first post and I hope I'm doing well, but if not, please let me know. My problem: First all classes and a main-file (I simplified everything to get to the core of the...
2
by: yangsuli | last post by:
It works perfectly well before but today my browser cannot get this php script i tried to run it in the localhost and it reported a seg fault :( i donot konw what to do.....:( restarting the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
1
by: dark lord | last post by:
extern void *malloc(); //structure of a token struct token { int tval; char *lexeme; struct token *link; }; struct token *tfirst=NULL; char *seentext;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.