473,473 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SEGFAULT when declaring input file with ifstream

10 New Member
Hi there,
I have to read in from file, and then process it. It looks like this.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. ...
  6. int main()
  7. {
  8. ifstream in_file ("list", ifstream::in);
  9. while (!in_file.eof()){
  10.         in_file.read(buffer,16);
  11.    ...
  12. }
  13. in_file.close();
  14. return 0;
  15. }
  16.  
It compiles OK, but when I run the program, it crashes with SEGFAULT on the line with ifstream in_file ("list", ifstream::in);
GDB shows:
Expand|Select|Wrap|Line Numbers
  1. Program received signal SIGSEGV, Segmentation fault.
  2. main () at HashTest.cpp:14
  3. 14      ifstream in_file ("list", ifstream::in);
and backtrace shows this stupid thing:
Expand|Select|Wrap|Line Numbers
  1. (gdb) backtrace full
  2. #0  main () at HashTest.cpp:14
  3.         buffer = "\bТъщВТ╞\004\b(&╬ЪP\212\004"
  4.         table = Cannot access memory at address 0xffbe2638
  5. (gdb)
What is the reason?:(
Jun 21 '09 #1
5 7454
newb16
687 Contributor
You omitter buffer variable definition; I declared buffer as char[100] and it worked.
Jun 22 '09 #2
Meetee
931 Recognized Expert Moderator Contributor
Segmentation faults are generally because a pointer of yours is either null or points to some random memory (probably never initialized) or points to memory that was deleted. You are storing read file in buffer which is not initialized and so it is showing error!
Jun 22 '09 #3
DrSchwartz
10 New Member
@zodilla58
But the buffer is already allocated, just after main(){
char buffer[16];
i just omitted that part when posting code snippet. so the reason should be different
Jun 22 '09 #4
JosAH
11,448 Recognized Expert MVP
@DrSchwartz
You should've posted a compiling, running and small example that exhibits the unwanted behaviour; now you've just shown us an incomplete piece of code that just leaves us guessing.

kind regards,

Jos
Jun 22 '09 #5
DrSchwartz
10 New Member
Thanks for your replies. OK, here is the code. I cannot imagine how I fault in this kind of plain simple program. (Note to avoid possible question: there IS file "list" in the directory of program)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include "GeneralHashFunctions.h"
  5. using namespace std;
  6. #define CHARBUFSIZE 16
  7. int main()
  8. {
  9. char buffer[CHARBUFSIZE];
  10. int i=0;
  11. ifstream in_file ("list", ifstream::in);
  12. if (in_file.fail() )
  13. {
  14.         cout << "Can't open input file" << endl;
  15.         return -1;
  16. }
  17. while (!in_file.eof()){
  18.         in_file.read(buffer,16);
  19.         std::string key = buffer;
  20.         std::cout<<RSHash(key)<<std::endl;
  21.         i++;
  22. }
  23. in_file.close();
  24. return 0;
  25. }
  26.  
One more thing, when I run program through GDB, it says:
warning: Lowest section in system-supplied DSO at 0xffffe000 is .hash at ffffe0b4
what does this mean?
Jun 22 '09 #6

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

Similar topics

4
by: pentiumPunk | last post by:
string fileName; cout<<"Enter filename of datafile you wish to open \n"; cin>>fileName; ifstream fin; fin.open(fileName, ifstream::ios); if(!fin) {
8
by: Jing Cheng | last post by:
Hi, I'm using ifstream reading data from a file, as following: ifstream finput("track.dat"); if(finput.fail()){ cerr << "Open input DATA file error!\n"; exit(-1);
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
9
by: malla | last post by:
Hi, I am trying to open a bunch of files and get data from them into one single file. I am sure this process can be automated if I name the files that I want to open in a regular pattern (say...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
4
by: amatoo04 | last post by:
hi guys, i am trying to finish up a program i started this morning and basically i got the whole input and output down... i just need help with the function which will scan the file and return number...
2
by: andrey.vul | last post by:
code: #include <iostream> #include <vector> #include <stdexcept> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>
3
by: Daniel T. | last post by:
This is basically what I have now... class Foo { /* definition irrelevant */ }; istream& operator>>( istream& is, Foo& foo ); // could throw int main() { ifstream file( "file.txt" ); Foo...
9
by: rory | last post by:
I'm having some inconsistencies in my program that I can't seem to debug. Below is code that makes a copy of a binary file, appends a unique ID and string to the copy of the binary file and then...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.