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

Problem With fgetc/getc

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 4371
gpraghuram
1,275 Expert 1GB
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
...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
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()...
6
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...
8
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)?...
3
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...
29
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
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...
27
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;...
3
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.