473,779 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ 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 7580
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
7279
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
4751
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
6091
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. #include <fstream> #include <iostream> #include <iomanip> using namespace std; struct pyrll
9
8864
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 1.txt, 2.txt, 3.txt, ...) Has anyone done sth like this before? Is there a way to achieve this easily? Please share.
7
4941
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 with VS2005. Please advice. Thank you.
4
2720
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 of lines, amount of characters, and amount of alphabetical characters. I was wondering if i would need 3 different functions, and how would i go about setting those up. here is my code so far /*Write a program, which scans a file and counts...
2
2416
by: andrey.vul | last post by:
code: #include <iostream> #include <vector> #include <stdexcept> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>
3
1705
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 foo; int i = 0;
9
1514
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 checks the string by searching for the ID which is also a string and retrieves the text that follows. I'm building the code with scons along with some other applications that are part of the project. Here is the strange thing. If I set a new unique...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
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
8959
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...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.