473,671 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem With fgetc/getc

2 New Member
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears now:

Expand|Select|Wrap|Line Numbers
  1. # include <stdio.h>
  2. # include <string.h>
  3.  
  4. int main(void)
  5. {
  6.     printf("Fine at line 6");
  7.     FILE *source;
  8.     char string1[10];
  9.     int safety=0;
  10.  
  11.     printf("Fine at line 11");    
  12.  
  13.     source = fopen("sourcetext.txt", "r" );
  14.     printf("Fine at line 16");
  15.     string1[0] = getc(source); //using fgetc yields the same result. Removal of this line results in the program running fine.
  16.     printf("%c",string1[0]);
  17.     fclose(source);
  18.     return 0;
  19. }
Of course lines 15 and 16 aren't what I want the program to do, they were just there to help me troubleshoot it. Unfortunately, it seems that if the program contains fgetc or getc it won't run, though cygwin compiles it fine. I get:
Expand|Select|Wrap|Line Numbers
  1. 17 [main] syntax 4100 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
  2. Segmentation fault (core dumped)
The program is called syntax at the moment, and it crashes no matter what the name is, with the name appearing after [main]. The two numbers are different every time I run the program. I got a stackdump file reading:
Expand|Select|Wrap|Line Numbers
  1. Exception: STATUS_ACCESS_VIOLATION at eip=610E67EE
  2. eax=0022D008 ebx=00000000 ecx=611010E8 edx=FFFFFFFF esi=611001A0 edi=00000064
  3. ebp=0022CC58 esp=0022CC40 program=c:\Users\OwnerHP\syntax.exe, pid 4100, thread main
  4. cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
  5. Stack trace:
  6. Frame     Function  Args
  7. 0022CC58  610E67EE  (00000000, 0040201F, 00401050, 00401075)
  8. 0022CCC8  61092D88  (00000001, 61169690, 00FA0090, 0022CC60)
  9. 0022CD78  61006198  (00000000, 0022CDB0, 61005510, 0022CDB0)
  10. 61005510  61004416  (0000009C, A02404C7, E8611001, FFFFFF48)
  11.      17 [main] syntax 4100 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
  12.  
The last time I worked with C code, this spring, I had the same problem, though there the program would run through 30 or so characters in the text file before crashing. It used getc or fgetc, I forget which, also.

What can I do to get this to run, and why is it crashing? I got a similar one to run a while earlier that took the characters retrieved from the file, counted them, and checked to see if there was a certain set, at which point it would stop and print how many characters it saw before the pattern at the end of the file.
Nov 28 '07 #1
2 4401
gpraghuram
1,275 Recognized Expert Top Contributor
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears now:

Expand|Select|Wrap|Line Numbers
  1. # include <stdio.h>
  2. # include <string.h>
  3.  
  4. int main(void)
  5. {
  6.     printf("Fine at line 6");
  7.     FILE *source;
  8.     char string1[10];
  9.     int safety=0;
  10.  
  11.     printf("Fine at line 11");    
  12.  
  13.     source = fopen("sourcetext.txt", "r" );
  14.     printf("Fine at line 16");
  15.     string1[0] = getc(source); //using fgetc yields the same result. Removal of this line results in the program running fine.
  16.     printf("%c",string1[0]);
  17.     fclose(source);
  18.     return 0;
  19. }
Of course lines 15 and 16 aren't what I want the program to do, they were just there to help me troubleshoot it. Unfortunately, it seems that if the program contains fgetc or getc it won't run, though cygwin compiles it fine. I get:
Expand|Select|Wrap|Line Numbers
  1. 17 [main] syntax 4100 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
  2. Segmentation fault (core dumped)
The program is called syntax at the moment, and it crashes no matter what the name is, with the name appearing after [main]. The two numbers are different every time I run the program. I got a stackdump file reading:
Expand|Select|Wrap|Line Numbers
  1. Exception: STATUS_ACCESS_VIOLATION at eip=610E67EE
  2. eax=0022D008 ebx=00000000 ecx=611010E8 edx=FFFFFFFF esi=611001A0 edi=00000064
  3. ebp=0022CC58 esp=0022CC40 program=c:\Users\OwnerHP\syntax.exe, pid 4100, thread main
  4. cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
  5. Stack trace:
  6. Frame     Function  Args
  7. 0022CC58  610E67EE  (00000000, 0040201F, 00401050, 00401075)
  8. 0022CCC8  61092D88  (00000001, 61169690, 00FA0090, 0022CC60)
  9. 0022CD78  61006198  (00000000, 0022CDB0, 61005510, 0022CDB0)
  10. 61005510  61004416  (0000009C, A02404C7, E8611001, FFFFFF48)
  11.      17 [main] syntax 4100 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
  12.  
The last time I worked with C code, this spring, I had the same problem, though there the program would run through 30 or so characters in the text file before crashing. It used getc or fgetc, I forget which, also.

What can I do to get this to run, and why is it crashing? I got a similar one to run a while earlier that took the characters retrieved from the file, counted them, and checked to see if there was a certain set, at which point it would stop and print how many characters it saw before the pattern at the end of the file.

HI,
I think the problem is not with getc/fgetc.
After opening the file check for NULL(for the file descriptor) and then do the file operations.
Only if the file descriptor is null then the segmentation fault happens.
I used a different file name for opening and reading.

Raghuram
Nov 29 '07 #2
Praesidium
2 New Member
...I had been using a different filename for my earlier test program, I changed the name in the code but forgot to change the file itself...
Thanks, I probably wouldn't have realized on my own: the two names were only one letter apart...
Nov 29 '07 #3

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

Similar topics

13
15116
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf() scanf()/fscanf()
6
2282
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's best to "think of it")? I've seen some explain it as if they call fgetc internally, then I've seen some people explain it as if they call some lower level OS system call like read(). I've even seen some older posts talk about a input functions...
8
17171
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
3
3339
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object.
29
3453
by: Markus Pitha | last post by:
Hello, I read a simple bmp-file with this loop: while ( !feof(fp) ) { printf("%x\n", fgetc(fp)); } fclose(fp); Everything seems to be correct, but at the end of the file, I get a weird
11
1928
by: kolmogolov | last post by:
hi, it's not really an endian problem. I think I must be missing something else ... The problem can be reduced to different results of the following two segments of codes: (cut and pasted verbatim) 1.
27
5097
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
3
4968
by: ANURAGBAJPAI | last post by:
The getc() is a macro while fgetc() is a function in the stdio.h library.Please tell me what is macro and explain the sentence.
3
9627
Parul Bagadia
by: Parul Bagadia | last post by:
getc gets the charachter from file stream and points to next charachter, fgetc gets the charachter from file stream and i don't know whether it points to next charachter or not; is that a significant change? In the help menu of Turbo C, it's given that its a function version of getc macro. what is a function version?
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8597
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.