create file, edit file.

Hello! Just want to let you know that this is my first topic on this forum.
I also want to apologize for my bad English. well, well.

To my issue.

Im a beginner at c++ so mayby it is a simple misstake here.
I can't get this code work correcly.

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

	
	system("cls");

	char filnamn[20];
	char text[100];
	char lookslike;


	cout<<"\nThen let's move on! "<<endl;
	cout<<"\nEnter the file name you want to create: ";
	cin.getline(filnamn, 20);
	ofstream f1(filnamn);
	cout<<"Enter the text you want to include the file: "<<endl;
	cin.getline(text, 100);
	f1 << text;
	f1.close();

	cout<<"You wanna see how the file you just created looks like? J/N";
	cin>>lookslike;

	if(lookslike=='J' && lookslike=='j'){
		ifstream f2(filnamn);
		cout<<"Detta står i filen\n"<<endl;
		cout<<f2.rdbuf();
		f2.close();


	}

	else if(lookslike!='J' && lookslike != 'j'){

		
		return 0;

	}


I got more code above this, and donät want to copy all. I got, #include <fstream><conio.h><windows.h><iostream><string> :)
closed account (o3hC5Di1)
Hi there,

Welcome to the forums :)

Please be a little more specific about what is not working.
If you get compiler errors, please include them, if you get unexpected behaviour, please tell us which behaviour you expect and which behaviour you actually get.

This will help us to help you more quickly :)

Thanks!

All the best,
NwN
Thanks!

I do not get any errors. The problem is that

cout<<"\nEnter the file name you want to create: ";
cout<<"Enter the text you want to include the file: "<<endl;

Will appear exacly after each other, and for some reason forget the file I shuld write in between these two. After the "couts" i can enter som text. It dosn't matter what i wrote in there. No file will be created eather. Once again sorry for my bad explanation :) ty
Last edited on
closed account (o3hC5Di1)
Hi there,

I suggest changing you code as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>

        char filnamn[20];
	char text[100];
	char lookslike;

        std::string filnamn, text, lookslike;

	cout<<"\nThen let's move on! "<<endl;
	cout<<"\nEnter the file name you want to create: ";
        cin >> filnamn;
	ofstream f1(filnamn);
	cout<<"Enter the text you want to include the file: "<<endl;
	cin >> text;


std::string is the preferred way to handle text-strings in C++, rather than char-arrays (C-style-strings).

They also allow for easy use of std::cin as shown above.

Hope that helps.

All the best,
NwN
Last edited on
Still don't work...when i run the program still enter following:

Enter the file name you want to create: Enter the text you want to include the file:
_



And when i try to put in std:: string filnamn, text, lookslike i got an massive error. :/
closed account (o3hC5Di1)
Your code should look like this:

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
system("cls");

        std::string filnamn, text;
        char lookslike;

	cout<<"\nThen let's move on! "<<endl;
	cout<<"\nEnter the file name you want to create: ";
	cin >> filnamn;
	ofstream f1(filnamn);
	cout<<"Enter the text you want to include the file: "<<endl;
	cin >> test;
	f1 << text;
	f1.close();

	cout<<"You wanna see how the file you just created looks like? J/N";
	cin>>lookslike;

	if(lookslike=='J' || lookslike=='j'){ //you mean || (OR) here, not && (AND)
 		ifstream f2(filnamn);
		cout<<"Detta står i filen\n"<<endl;
		cout<<f2.rdbuf();
		f2.close();


	}

	else if(lookslike!='J' || lookslike != 'j'){ //you mean || (OR) here, not && (AND)

		
		return 0;

	}


If that throws you compiler errors, please copy-paste them here.

All the best,
NwN
Ye now it works! :) Ty! Yeah some misstakes there (||) .
But, if i do not use cin.getline i can't wrote as long text as i want.
string = only one word. But yes, the function works fine now!
Topic archived. No new replies allowed.