|
-
February 21st, 2012, 03:46 PM
#1
ifstream.open() doesn't open file
I have some code that opens a file into a ifstream and writes each line to a vector<string>.
Code:
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void read_output_columns_file(string& output_columns_file) {
// create an input stream and open the colums_input_file file
ifstream output_columns_file_istream;
string new_input_line;
output_columns_file_istream.open( output_columns_file.c_str() );
// check to see if the file is open
if(!output_columns_file_istream.is_open()) {
cout << "file " << output_columns_file << " could not be opened" << endl;
exit(-4);
}
// remove unwanted characters and save data from file in vector
vector<string> inputs_from_file;
while( getline(output_columns_file_istream, new_input_line) ) {
// remove '\r' EOL chars and tab characters
new_input_line.erase (remove(new_input_line.begin(),new_input_line.end(),'\r'), new_input_line.end());
new_input_line.erase (remove(new_input_line.begin(),new_input_line.end(),'\t'), new_input_line.end());
// add line to vector of string
inputs_from_file.push_back(new_input_line);
}
// close istream
output_columns_file_istream.close();
return;
]
I am getting my trap error that the file was not opened. This worked yesterday, but I had things all in one file. Today, I broke this into a few src files and wrote a make. It compiles and links, but I get this error. The next thing to happen is that inputs_from_file gets parsed to some other locations, and that code gives a stackdump because the vector is empty.
I also tried this with a very simple build, g++ -c, then g++ -o, and I get the same thing, so I don't think it's a problem with the make. Below is the make file in case that could be the issue. I have printed out the file name, and that is correct, it doesn't appear to be a permission issue with the file, etc.
Any suggestions?
LMHmedchem
Code:
# 12-02-21 makefile for datasplit.exe
SOURCELOC = ./src
# create build location name
OS := $(shell uname -s)
ARCH := $(shell uname -m)
KERN := $(shell uname -r | cut -d. -f 1,2)
CMP :=g34
BDIR := bld_$(OS)_$(CMP)_$(KERN).$(ARCH)
# compiler falgs
CC++ = g++
CFLAGS = $(OPTIMIZE) -Wall
OPTIMIZE = -O2
LIBS = -lstdc++
## create build directory if it doesn't exist
archdir: $(BDIR)
$(BDIR):
@mkdir -p $(BDIR)
all: $(BDIR)/datasplit.exe
FORCE:
clean:
rm $(BDIR)/*.o $(BDIR)/*.exe
# data_split objects
OBJS = \
$(BDIR)/datasplit_main.o \
$(BDIR)/datasplit_read_file.o \
$(BDIR)/datasplit_print.o
# build datasplit.exe
$(BDIR)/datasplit.exe: $(OBJS)
$(CC++) $(OPTIMIZE) -o $@ $(FCFLAGS) $^ $(LIBS)
# compile src c++ objects
$(BDIR)/%.o: $(SOURCELOC)/%.cpp
$(CC++) $(CFLAGS) -c -o $@ $<
## force recompilation of files if header is changed
$(SOURCELOC)/*.cpp: $(SOURCELOC)/columns.h \
$(SOURCELOC)/datasplit_read_file.h \
$(SOURCELOC)/datasplit_print.h
touch $@
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|