Please help me!! I am new to C++
Here is a program helped me little bit.

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

#include <iostream>
#include <fstream>
using namespace std;

main () {

  int filename;
  cout << "Enter file name"; cin >> filename;

    
  
  ofstream myfile;
  myfile.open ("filename.txt");               
  myfile << "Writing this to a file.\n";
  myfile << "000" << filename;            
  myfile.close();
  return 0;
}

--------------------------------------------------------------------------
The above is only a sample, it does not work, Help to resolve this issue.

Let me explain how the program should?

1. I will enter a file name, then the program should add "000" three zeros before the filename, and should create a .txt file.

for example if I enter the file name as "1234567" (only digits), It should create "0001234567_OP.txt"

I can manage to put data on it.

Please help me any C++ experts, I am very new on this.

Recommended Answers

All 11 Replies

Next time please use code tags

#include <iostream>
#include <fstream>
using namespace std;

main () {

int filename;
cout << "Enter file name"; cin >> filename;



ofstream myfile;
myfile.open ("filename.txt"); 
myfile << "Writing this to a file.\n";
myfile << "000" << filename; 
myfile.close();
return 0;
}

There are a lot of errors in your code.

Firstly

int filename;
The above line creates an integer as filename , So How can it hold the value of a string of characters.

I recommend that you use String there.

Secondly.

myfile.open ("filename.txt");

This line opens a file known as filename.txt but not the desired file.

For this you should consider using the + operator for strings;

Here is an example that will clear you out.

string x;
x="string";//Value of x is "string"
x=x+".txt";//Now value of string is "string.txt"
ofstream file;
file.open(x.c_str());//c_str() will return a c-styled string of char[] so file will open "string.txt"

Now following the example above figure out your requirements :)

>>for example if I enter the file name as "1234567" (only digits), It should create "0001234567_OP.txt"

#include <sstring>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int num = 12345;
    string filename;
    stringstream str;
    str << num; // converts num to a string
    filename = "000";
    filename += str.str();
    filename += "_OP.txt"";

   ofstream out(filename.c_str());

>>for example if I enter the file name as "1234567" (only digits), It should create "0001234567_OP.txt"

#include <sstring>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int num = 12345;
    string filename;
    stringstream str;
    str << num; // converts num to a string
    filename = "000";
    filename += str.str();
    filename += "_OP.txt"";

   ofstream out(filename.c_str());

Thanks for trying. Bad for me, I am unable to to figure out the problem, I get lots of errors.

I have Turboc C++ ver 3.0

Errors:
2: Unable to open include file 'sstring.h'
4: Declaration syntax error
9: Undefined symbol 'sstring'
9: Statement missing ;
10: Undefined symbol 'stringstream'
10: Statement missing ;
11: Undefined symbol 'str'
12: Undefined sysmbol 'filename'
18: Function should return a value
18: 'num' is assigned a value that is never used

sorry to trouble you again. please let me know if this will work in turboc C++ ver 3.0, or I need to do this in other compiler.

Thanks.

>>I have Turboc C++ ver 3.0

Don't use that crappy compiler because its too old. google for Code::Blocks or VC++ 2008 Express. Even Borland C++ Builder 5.0 would probably work.

I'm not going to write it as if this were 30+ years ago. Get into the 21st Century like everyone else on the planet.

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int num = 12345;
    string filename;
    stringstream str;
    str << num; // converts num to a string
    filename = "000";
    filename += str.str();
    filename += "_OP.txt";
	cout<<filename;
   ofstream out(filename.c_str());
}

This is the code.
GCC/g++ doesnt seem to have <sstring> , I used <sstream> instead.
As Stringstreams are in sstream;

Also added an additional cout statement. And cleared the error of ' " ' extra in

filename += "_OP.txt"";

GCC/g++ doesnt seem to have <sstring> , I used <sstream> instead.

Yes, sstring was an error and should have been sstream.

I'm not going to write it as if this were 30+ years ago. Get into the 21st Century like everyone else on the planet.

I wonder why some schools still insist on using Turbo when everyone in real life know that it's a crappy-nonstandard-outdated compiler. :-/
Can someone please explain? I mean Code::block and Visual Studio are free! What reason could they possibly have?

>I wonder why some schools still insist on using Turbo when everyone in real life
>know that it's a crappy-nonstandard-outdated compiler.
>Can someone please explain?
It is called the we-first-teach-students-basic-and-then-move-to-advanced-compiler philosophy but they often forgets that it is very hard for students to quite that crap.
Or may be they want to realize the student how much crap that compiler is and then make them switch to heaven.

>I mean Code::block and Visual Studio are free!
Linux is also free but you see M$ Operating system around <noflame> those are also no less than crap </noflame>.

Visual Studio is not free. It is just that it is zero-cost. Free means this

I wonder why some schools still insist on using Turbo when everyone in real life know that it's a crappy-nonstandard-outdated compiler. :-/
Can someone please explain? I mean Code::block and Visual Studio are free! What reason could they possibly have?

As far as my experience goes. And lol, it not a long one ....... and especially in a lot of schools in my country. The Management only offer computer programming as a certain bonus. STATE -Country Syllabi(Syllabusses) Have yet to Add Computer Programming as a requirement of any sort.

In my school a computer class was not for teaching computer science. But they made us play games on the computers.

In my school a computer class was not for teaching computer science. But they made us play games on the computers.

Nothing wrong with that either -- there are lots of people who have never used a computer, so that type if class may be necessary to get them introduced to computers. We have those types of classes here in USA also.

//create a text file with comand line argument(Turbo c++)

	#include<fstream.h>
	#include<iostream.h>
	int main(int argc, char *argv[])
	{
		if(argc!=2)
		{
			cout<<"Syntext error.....!";
			cout<<"Typing should be: create filename"<<endl;
			return(0);
		}
		ofstream outf(argv[1]);
		char line[80];
            cout<<"Enter a string:(Programing should be tarminate by press !')\n";
	do
		{
			cin.getline(line,80);
			outf<<line<<endl;
		}while(*line != '!');
      cout<<"Create the file "<<argv[1]<<" and saved in disk.";
		outf.close();
      return(0);
	}
commented: bump 2 year old thread -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.