ios::app and ios::_Nocreate not going alone ?

Hi, I'm trying to open a file with ios::app, but without creating it if it doesn't exist. What is wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main(){
	
	
	fstream file("lm.txt",ios::in|ios::out|ios::app|ios::_Nocreate);
	if(!file.is_open()){
		cout<<"No file";
		
	}

cin.sync();
cin.ignore();
return 0;
}

What error messages are you getting?

Why are you trying to use the app open mode?

Do you really need all output operations to happen at the end of the file? If you aren't re-positioning the output stream between outputs then the default open mode will probably be sufficient. Also be careful, there are several failure modes that can triggered when using the app mode, for example using ios::in and ios::app can cause the opening of the stream to fail.

Last edited on
I'm not getting an error message, but the file gets created.
I try to open in app mode, to write at the end of the file, which already has some content inside.
Files opened with ios::app are equivalent to files opened with "a" with std::fopen and the file is always created (provided it can be) if it does not exist. Your implementation likely ignores the contradictory enumeration you supply.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void echo(std::istream& is, std::ostream& os)
{
    std::string line;

    while (std::getline(is, line))
        os << line << '\n';
}

int main(){

    fstream file("lm.txt");

    if (!file.is_open()){
        cout << "No file";
    }
    else
    {
        file.close();
        file.open("lm.txt", std::ios::in | std::ios::app);
    }

    file << "added text.\n" << std::flush;

    file.seekg(0);  // intervening seek between write/read.
    echo(file, std::cout);
}


It's pretty rare that you want to open a file for both reading and writing. What are you planning on doing?
Last edited on
Then I recommend that you use the default open mode. The other option is to use the ape mode, as long as you don't re-position the output stream you should be good.

Also be careful using the ios::app mode with just the ios::in mode, that will cause the stream to fail if you happen to be using a C++98 compiler.


But if you don't require input and output on the stream I really suggest you use either the ifstream or ofstream class instead. Using fstream when not required is a pain, IMO.

I tried some kamikadze stuff and it worked this way : fstream file("lm.txt",ios::in|ios::out|ios::app&ios::_Nocreate);. Not sure how safe it is, but it did the job.

The program takes two doubles, NELM and TELM(astronomy stuff, limiting magnitude for naked eye and for telescope) and tells how much greater TELM is comparing to NELM.

Then it opens a file, which contains two doubles per line(NELM and TELM for other nights) and tells the average NELM and TELM.
At the end, or if the files dosn't exist or is empty it asks you if you want to add your entered NELM and TELM to the file.
Here is the code, I think it's finished:
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

#define endi cin.sync();cin.ignore();return 0

using namespace std;



int main(){
	
	double nelm,telm,plus,times,percentage;
	char c;
	
	cout<<"NELM : ";
	cin>>nelm;
	cout<<"TELM : ";
	cin>>telm;

	plus=telm-nelm;
	times=telm/nelm;
	percentage=((100*telm)/nelm)-100;

	cout<<endl<<endl;
	cout<<"TELM = "<<plus<<" + NELM("<<nelm<<")"<<endl;
	cout<<"TELM = "<<times<<" * NELM("<<nelm<<")"<<endl;
	cout<<"TELM = "<<percentage<<"% + NELM("<<nelm<<")"<<endl;

	fstream file("lm.txt",ios::in|ios::out|ios::app&ios::_Nocreate);
	if(!file.is_open()){
		cout<<"No file, create and add entry ? (Y/N) : ";
		cin>>c;
		if(c!='Y'&&c!='y'){
			endi;
		}
		else{
			fstream file("lm.txt",ios::out);
			file<<nelm<<" "<<telm<<endl;
			cout<<"Added.";
			endi;
		}


	}

	string str;
	int i=0;
	double avNelm=0,avTelm=0,tempNelm,tempTelm;

	getline(file,str);

	if(!str.empty() && isdigit(str.at(0))){
		file.seekg(0,ios::beg);
	}
		

	while(file>>tempNelm && file>>tempTelm){
		avNelm+=tempNelm;
		avTelm+=tempTelm;
		i++;
	}
	
	cout<<endl<<endl;

	if(i==0){
		cout<<"No or unvalid entries, add entry ? (Y/N) : ";
		cin>>c;
		if(c=='Y'|c=='y'){
			file.clear();
			file<<nelm<<" "<<telm<<endl;
			cout<<"Added.";
		}
		endi;
	}
	
	cout<<"-------------------------------------------------------------------------------"<<endl<<endl<<endl;

	avNelm/=i;
	avTelm/=i;

	cout<<"Averaga NELM = "<<avNelm<<endl;
	cout<<"Average TELM = "<<avTelm<<endl<<endl;

	double avPlus,avTimes,avPercentage;
	

	avPlus=avTelm-avNelm;
	avTimes=avTelm/avNelm;
	avPercentage=((100*avTelm)/avNelm)-100;

	cout<<"AvTELM = "<<avPlus<<" + AvNELM("<<avNelm<<")"<<endl;
	cout<<"AvTELM = "<<avTimes<<" * AvNELM("<<avNelm<<")"<<endl;
	cout<<"AvTELM = "<<avPercentage<<"% + AvNELM("<<avNelm<<")"<<endl;

	cout<<endl<<endl<<"Add entry ? (Y/N) : ";
	cin>>c;
	
	if(c=='Y' || c=='y'){
		file.clear();
		file<<nelm<<" "<<telm<<endl;
		cout<<"Added.";
	}


	cin.sync();
	cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.