Program is running but cannot open text file

Hi coders. I am writing a college project. The program is compiling fine but it wont create an fstream object to take input and enter to "Land_registration.txt" file.

Kindly advise what error i have committed where i have indicated in the commentary within the below program. The program should enter string data into a two dimensional vector at runtime. The columns have titles e.g.LR_UNIT_NUMBER, NAME_OF_PROPRIETOR,...etc. The data should be entered into new records row-wise (nrows) during runtime and then the program displays what has been entered into the against the specific column_name.

I would like to write the data entered at runtime from the vector into a text file.




#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>

using namespace std;
fstream myFile ("Land_registration.txt", ios::in ||ios::out ||ios::app);

//creating an fstream object to open/ create a text file into which the vector information will be saved to.

std::string input ; //Declaring a variable into which will hold input entered at runtime.

std::istream& get_non_empty_line( std::istream& stm, std::string& line )
{
//passing line variable by reference. This function will read input from keyboard and enter to memory

if( std::getline( stm, line ) )

if( line.empty() )
return get_non_empty_line(stm,line) ;
return stm ;
}

std::istream& get_row( std::istream& istm, std::vector<std::string>& row,
const std::vector<std::string>& Column_names, std::ostream& ostm = std::cout )
{ //passing 1D vectors row and Column_names by reference.
//function that takes keyboard input into row vector against the Column_names aray elements at runtime

row.clear() ;
ostm << '\n' ;
for( std::size_t i = 0 ; i < Column_names.size() ; ++i )
{


ostm << "Enter " << Column_names[i] << ": " ; //display Column_names array
if( get_non_empty_line( istm, input ) ) //This function will input strings at runtime into the declared variable, named input

row.push_back(input) ; //populating row vector dynamically

else { row.clear() ; return istm ; }
}

return istm ;
}

int main()
{



const char* const Column_names_array[] = { "LR_UNIT_NUMBER", "NAME_OF_PROPRIETOR", "NATIONAL_ID_CARD_NUMBER", "ADDRESS_OF_PROPRIETOR", "PIN_NUMBER_OF_PROPRIETOR", "DATE_LAND_WAS_ACQUIRED", "DATE_OF_ISSUE_OF_TITLE_DEED",
"LAND_CATEGORY" /* etc.*/ } ; //initialized 1D array

const std::size_t n_Column_names = sizeof(Column_names_array) / sizeof(Column_names_array[0]) ;

const std::vector<std::string> Column_names( Column_names_array, Column_names_array + n_Column_names) ; //1D string vector declaration and initialization using 1D char Column_names array elements

std::vector< std::vector<std::string> > data_set ; //2D string vector declared

std:: size_t nrows ;
std::cout << "number of records? " ;
std::cin >> nrows ;

std::vector<std::string> row ; //1D string vector declared

while( data_set.size() < nrows && get_row( std::cin, row, Column_names) ) //to fill the 2D vector data_set when iterating through row to row
{
data_set.push_back(row) ; //populating the 2D data_set vector using the 1D row vector

for( std::size_t i = 0 ; i < data_set.size() ; ++i )
{
const std::vector<std::string>& row = data_set[i] ; //data_set vector is assignde to address of row vector by reference.

for( std::size_t i = 0 ; i < Column_names.size() ; ++i )

if (myFile.is_open ()) //checking whether the myFile object/handle opened the text file into which runtime input will be saved <-----problem here

{ myFile<< Column_names [i] << ": " << row[i]<<'n'; //if open, the fstream object should write the input to text file "Land_registration.txt". <-----problem here


std::cout<<"\n\nThe information has been successfully captured and entered to Land_registration file !"<<std::endl;
myFile.close ();}
}

if (myFile.fail()) //Bit flag, if stream object not open, it display this
{std::cout<<"\n\nLand_registration file could not open !\n\nInformation could not be saved to Land_registration file !\n\n"<<std::endl;
myFile.close ();}

{
std::cout << "\n--------The Land record information you typed is :---------\n\n" ; //displaying information entered on keyboard and stored to memory


for( std::size_t i = 0 ; i < data_set.size() ; ++i )
{
const std::vector<std::string>& row = data_set[i] ;

for( std::size_t i = 0 ; i < Column_names.size() ; ++i ) {

std::cout << Column_names [i] << ": " << row[i]<<'n'<<std::endl;


}
std::cout<<std::endl<< "--------------------------------------------------\n" ;


}
}}


system ("pause");

return 0;
}
Last edited on
Please edit your post and use code tags to make it readable - http://www.cplusplus.com/articles/jEywvCM9/

Edit: I love it when people just ignore important rules such as using code tags even tho we specifically tell them to.
Last edited on
One problem is ios::in ||ios::out ||ios::app

The openmode is a bitfield and you need to to use | instead of ||.

Second problem is that you close the file and then check for fail().

1
2
3
4
5
6
7
8
9
std::cout<<"\n\nThe information has been successfully captured and entered to 
Land_registration file !"<<std::endl;
myFile.close ();}
}

if (myFile.fail()) //Bit flag, if stream object not open, it display this
{std::cout<<"\n\nLand_registration file could not open !\n\nInformation could not be saved to 
Land_registration file !\n\n"<<std::endl;
myFile.close ();}






I am grateful Thomas for the correction ! It is now doing.

The program is writing only one row to the text file.What could be the problem in this loop ?



for( std::size_t i = 0 ; i < data_set.size() ; ++i )
{
const std::vector<std::string>& row = data_set[i] ; //data_set vector is assignde to address of row vector by reference.

for( std::size_t i = 0 ; i < Column_names.size() ; ++i )

if (myFile.is_open ()) //checking whether the myFile object/handle opened the text file into which runtime input will be saved

{ myFile<< Column_names [i] << ": " << row[i]<<'n'; //if open, the fstream object should write the input to text file "Land_registration.txt".


std::cout<<"\n\nThe information has been successfully captured and entered to Land_registration file !"<<std::endl;
myFile.close ();}
When I ran it and entered two rows it wrote two rows.
number of records? 2

Enter LR_UNIT_NUMBER: 1
Enter NAME_OF_PROPRIETOR: 1
Enter NATIONAL_ID_CARD_NUMBER: 1
Enter ADDRESS_OF_PROPRIETOR: 1
Enter PIN_NUMBER_OF_PROPRIETOR: 1
Enter DATE_LAND_WAS_ACQUIRED: 1
Enter DATE_OF_ISSUE_OF_TITLE_DEED: 1
Enter LAND_CATEGORY: 1

The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !

--------The Land record information you typed is :---------

LR_UNIT_NUMBER: 1n
NAME_OF_PROPRIETOR: 1n
NATIONAL_ID_CARD_NUMBER: 1n
ADDRESS_OF_PROPRIETOR: 1n
PIN_NUMBER_OF_PROPRIETOR: 1n
DATE_LAND_WAS_ACQUIRED: 1n
DATE_OF_ISSUE_OF_TITLE_DEED: 1n
LAND_CATEGORY: 1n

--------------------------------------------------

Enter LR_UNIT_NUMBER: 2
Enter NAME_OF_PROPRIETOR: 2
Enter NATIONAL_ID_CARD_NUMBER: 2
Enter ADDRESS_OF_PROPRIETOR: 2
Enter PIN_NUMBER_OF_PROPRIETOR: 2
Enter DATE_LAND_WAS_ACQUIRED: 2
Enter DATE_OF_ISSUE_OF_TITLE_DEED: 2
Enter LAND_CATEGORY: 2


The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !
The information has been successfully captured and entered to Land_registration file !

--------The Land record information you typed is :---------

LR_UNIT_NUMBER: 1n
NAME_OF_PROPRIETOR: 1n
NATIONAL_ID_CARD_NUMBER: 1n
ADDRESS_OF_PROPRIETOR: 1n
PIN_NUMBER_OF_PROPRIETOR: 1n
DATE_LAND_WAS_ACQUIRED: 1n
DATE_OF_ISSUE_OF_TITLE_DEED: 1n
LAND_CATEGORY: 1n

--------------------------------------------------
LR_UNIT_NUMBER: 2n
NAME_OF_PROPRIETOR: 2n
NATIONAL_ID_CARD_NUMBER: 2n
ADDRESS_OF_PROPRIETOR: 2n
PIN_NUMBER_OF_PROPRIETOR: 2n
DATE_LAND_WAS_ACQUIRED: 2n
DATE_OF_ISSUE_OF_TITLE_DEED: 2n
LAND_CATEGORY: 2n
[/code]

File content:
LR_UNIT_NUMBER: 1nNAME_OF_PROPRIETOR: 1nNATIONAL_ID_CARD_NUMBER: 1nADDRESS_OF_PROPRIETOR: 1nPIN_NUMBER_OF_PROPRIETOR: 1nDATE_LAND_WAS_ACQUIRED: 1nDATE_OF_ISSUE_OF_TITLE_DEED: 1nLAND_CATEGORY: 1nLR_UNIT_NUMBER: 1nNAME_OF_PROPRIETOR: 1nNATIONAL_ID_CARD_NUMBER: 1nADDRESS_OF_PROPRIETOR: 1nPIN_NUMBER_OF_PROPRIETOR: 1nDATE_LAND_WAS_ACQUIRED: 1nDATE_OF_ISSUE_OF_TITLE_DEED: 1nLAND_CATEGORY: 1nLR_UNIT_NUMBER: 2nNAME_OF_PROPRIETOR: 2nNATIONAL_ID_CARD_NUMBER: 2nADDRESS_OF_PROPRIETOR: 2nPIN_NUMBER_OF_PROPRIETOR: 2nDATE_LAND_WAS_ACQUIRED: 2nDATE_OF_ISSUE_OF_TITLE_DEED: 2nLAND_CATEGORY: 2n
Yes Thomas. But i want it to copy the same information it outputs to screen i.e. for your case, if i open the Land_registration file i should find:


LR_UNIT_NUMBER: 1n
NAME_OF_PROPRIETOR: 1n
NATIONAL_ID_CARD_NUMBER: 1n
ADDRESS_OF_PROPRIETOR: 1n
PIN_NUMBER_OF_PROPRIETOR: 1n
DATE_LAND_WAS_ACQUIRED: 1n
DATE_OF_ISSUE_OF_TITLE_DEED: 1n
LAND_CATEGORY: 1n

--------------------------------------------------
LR_UNIT_NUMBER: 2n
NAME_OF_PROPRIETOR: 2n
NATIONAL_ID_CARD_NUMBER: 2n
ADDRESS_OF_PROPRIETOR: 2n
PIN_NUMBER_OF_PROPRIETOR: 2n
DATE_LAND_WAS_ACQUIRED: 2n
DATE_OF_ISSUE_OF_TITLE_DEED: 2n
LAND_CATEGORY: 2n


With the code i have now, if i choose one record it only write to Land_registration file :

LR_UNIT_NUMBER: 1n


so it stops there and leaves out the rest of

NAME_OF_PROPRIETOR: 1n
NATIONAL_ID_CARD_NUMBER: 1n
ADDRESS_OF_PROPRIETOR: 1n
PIN_NUMBER_OF_PROPRIETOR: 1n
DATE_LAND_WAS_ACQUIRED: 1n
DATE_OF_ISSUE_OF_TITLE_DEED: 1n
LAND_CATEGORY: 1n

--------------------------------------------------
Last edited on
Here is the way I would do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>

typedef std::vector<std::string> DataRow;
typedef std::vector<DataRow> DataSet;

void SaveDataSet(DataSet& data, std::fstream& dest);
void SaveDataRow(DataRow& row, std::fstream &dest);

void PrintDataSet(DataSet& data);
void PrintDataRow(DataRow& row);

bool GetData(DataSet& data);
bool GetDataRow(DataRow& row);

const char* FIELD_NAMES[] = 
{ 
  "LR_UNIT_NUMBER", "NAME_OF_PROPRIETOR", "NATIONAL_ID_CARD_NUMBER", 
  "ADDRESS_OF_PROPRIETOR", "PIN_NUMBER_OF_PROPRIETOR", "DATE_LAND_WAS_ACQUIRED", 
  "DATE_OF_ISSUE_OF_TITLE_DEED",
  "LAND_CATEGORY"  
}; 

const int NUM_FIELDS = sizeof(FIELD_NAMES) / sizeof(FIELD_NAMES[0]);

int main()
{
  std::fstream myFile ("Land_registration.txt", 
                       std::ios::in | std::ios::out | std::ios::app);

  if (!myFile)
  {
    std::cerr << "\a\aError creating file - Program will terminate" << "\n\n";
    exit(EXIT_FAILURE);
  }

  DataSet data;
  if (GetData(data))
  {
    PrintDataSet(data);
    SaveDataSet(data, myFile);
  }
  else
  {
    std:: cout << "No data was entered - Program will terminate" << "\n\n";
  }
  system ("pause");

  return 0;
}

void SaveDataSet(DataSet& data, std::fstream& dest)
{
  for (size_t i = 0 ; i < data.size() ; ++i)
  {
    SaveDataRow(data[i], dest);
  }
}

void SaveDataRow(DataRow& row, std::fstream &dest)
{
  for (size_t col = 0; col < row.size(); ++col)
  {
    dest << FIELD_NAMES[col] << ": " << row[col] << 'n' << '\n'; 
  }
}

void PrintDataSet(DataSet& data)
{
  std::cout << "\n--------The Land record information you typed is :---------\n\n";

  for(size_t i = 0; i < data.size(); ++i)
  {
    PrintDataRow(data[i]);
  }
}

void PrintDataRow(DataRow& row)
{
  for(size_t i = 0 ; i < NUM_FIELDS ; ++i ) 
  {
    std::cout << FIELD_NAMES[i] << ": " << row[i] << 'n' << std::endl;
  }
  std::cout << "\n--------------------------------------------------\n" ;
}

bool GetData(DataSet& data)
{
  size_t num_rows;

  std::cout << "Number of records - 0 to terminate: " ;
  std::cin >> num_rows;

  if(num_rows == 0)
    return false;

  for (size_t row = 0; row < num_rows; row++)
  {
    DataRow dataRow;
    if (!GetDataRow(dataRow))
      return false;
    data.push_back(dataRow);
  }
  return true;
}

bool GetDataRow(DataRow& row)
{
  std::string input;
  std::cout << "--------------------------------------\n";
  for (std::size_t i = 0 ; i < NUM_FIELDS ; ++i)
  {
    std::cout << "Enter " << FIELD_NAMES[i] << ": " ; 
    std::cin >> input;
    
    if (input == "")
      return false;

    row.push_back(input) ; //populating row vector dynamically
  }
  return true;
}


Play around with the code and see if you need to adapt it.
Wooow ! Thanx Thomas ! That was wonderful indeed. There has been power black out all day and have been following the thread with hope to check who wil haul me up.

I will play around with it to even add more functions like the find ().

Thanx for your time and effort bro.
Topic archived. No new replies allowed.