Reads only 9 characters?

No file is created during runtime even when path is specified?//solved. New error on post 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
	system("cls");
	char pathin[500];
	cout << "Enter path : ";
	cin.getline(pathin,'\n');
	char *path = new char[strlen(pathin)+1];
	int i;
	for (i = 0; i <= (signed)strlen(pathin); i++)
	{
		path[i] = pathin[i];
	}
	path[i] = '\0';
	ofstream fout(path,ios::_Noreplace);
	fout << path;
	fout.close();
	system("pause");
	return 0;
}


Edit: see post 5
Last edited on
To copy a C string, use strdup(), but you actually don't need to copy it a tall, Just pass pathin to the ofstream constructor.

It looks like you're trying to avoid opening an existing file, but ios::_Noreplace is non standard. See this link for some discussion: http://stackoverflow.com/questions/9251581/c-substitution-of-iosnoreplace

If your system definitely supports _Noreplace, then you probably need to add ios::out to the mode also:
ofstream fout(path,ios::_Noreplace | ios::out);
Oh, thanks for pointing it out. Actually I copied the string for just adding a '\0' in the end. :p
Strange this does not sorts out the problem.
Ok, seems that creating a file at C:\ is prohibited but there is some problem with string input too. Its not reading more than 9 characters.
What are you entering?


Last edited on
seems that creating a file at C:\ is prohibited
What permissions does your program have? If you try to create file manually does UAC ask for a prompt?

Do not create files where you are not supposed to. There are several places intended for application storage. Use them.
If I enter suppose "E:\Newfile.txt"
then only "E:\Newfil" is taken as input. No idea why. Neither I have idea about hot to mess with permissions within the code.
Second argument of std::istream::getline is target buffer size (maximum line size + terminating 0). '\n' has character value of 10.
Last edited on
Topic archived. No new replies allowed.