HelpCreate multiple files from a variable.

Posts 1–15 of 19 · Page 1 of 2
Create multiple files from a variable.
I'm "working" on a program that can output one or more files on the desktop,with possibly different extensions,starting from a variable.

This means an user can choose to create X number of files on the desktop.
ATM the code looks like this:
Code:
using namespace std;
int main()
{
int x,y,n=1;
cin>>x;
for(y=0;y<=x;y++)
{
     //create file named as n on the desktop (first cycle it'd be 1.txt,second 2.txt etc etc)
     n++;
}
system("pause");
return 0;
}
Anyone knows what i should put in the comment line?
Usa la libreria <fstream>, crea una variabile fstream e apri lo stream con come nome il valore di n (l'estensione decidila tu) poi lo richiudi subito. Ogni volta che apri lo stream con path diverso (valore di n) creerà un file vuoto nuovo.

Ah già, devi specificare il path del desktop.


Edit:

Ho pensato che dato che non controllerò spesso il post, ti metto in spoiler il codice che necessita di piccole modifiche per adattarlo alle tue necessità.
Se vuoi vederlo è qui sotto:

 
Studia o... copia :D
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

fstream prova; //variabile per lo stream all'interno di un file

int main()
{
int x,y,n=1;
cin>>x;
char nome[10] = "0.txt"; //nome del file che funziona da path (specificare quella del desktop includendo il nome del file)
//allunga la dimensione necessaria per contenere tutto l'indirizzo fino al desktop
for(y=0;y<=x;y++)
{
nome[0] = (char)(48+n); //in questo caso l'indice vale 0 perchè il nome del file (n, che viene convertito in carattere aggiungendo 48 che è lo 0 nella tabella ASCII)
//quando metti il path del desktop, conta che numero di carattere è il nome del file e sostituiscilo allo 0

prova.open(nome, ios::app); //apro lo stream

prova.close(); //chiudo lo stream
n++;
}
system("pause");
return 0;
}
Quote Originally Posted by Sixx93 View Post
Usa la libreria <fstream>, crea una variabile fstream e apri lo stream con come nome il valore di n (l'estensione decidila tu) poi lo richiudi subito. Ogni volta che apri lo stream con path diverso (valore di n) creerà un file vuoto nuovo.

Ah già, devi specificare il path del desktop.


Edit:

Ho pensato che dato che non controllerò spesso il post, ti metto in spoiler il codice che necessita di piccole modifiche per adattarlo alle tue necessità.
Se vuoi vederlo è qui sotto:

 
Studia o... copia :D
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

fstream prova; //variabile per lo stream all'interno di un file

int main()
{
int x,y,n=1;
cin>>x;
char nome[10] = "0.txt"; //nome del file che funziona da path (specificare quella del desktop includendo il nome del file)
//allunga la dimensione necessaria per contenere tutto l'indirizzo fino al desktop
for(y=0;y<=x;y++)
{
nome[0] = (char)(48+n); //in questo caso l'indice vale 0 perchè il nome del file (n, che viene convertito in carattere aggiungendo 48 che è lo 0 nella tabella ASCII)
//quando metti il path del desktop, conta che numero di carattere è il nome del file e sostituiscilo allo 0

prova.open(nome, ios::app); //apro lo stream

prova.close(); //chiudo lo stream
n++;
}
system("pause");
return 0;
}
problema 1: Il codice si ferma a 155 elementi (e già si capisce la natura malvagia del programma)
problema 2: una volta raggiunto il 9 passa alle lettere,mentre vorrei che andasse a 11,12,13...

Ho provato a modificare il codice ma boh D:
Quote Originally Posted by IV2B View Post
problema 1: Il codice si ferma a 155 elementi (e già si capisce la natura malvagia del programma)
problema 2: una volta raggiunto il 9 passa alle lettere,mentre vorrei che andasse a 11,12,13...

Ho provato a modificare il codice ma boh D:
Il problema 1 non saprei, dovrei pensarci, magari domani provo a vedere qualcosa che ora sono impegnato.

Il problema 2 è palese, nella tabella ASCII dopo il 9 penso ci sia la 'a' o la 'A'... Devi modificartelo un pò il codice, ma se te lo facessi io non avrebbe senso perchè non impareresti u.u
Quote Originally Posted by Sixx93 View Post
Il problema 1 non saprei, dovrei pensarci, magari domani provo a vedere qualcosa che ora sono impegnato.

Il problema 2 è palese, nella tabella ASCII dopo il 9 penso ci sia la 'a' o la 'A'... Devi modificartelo un pò il codice, ma se te lo facessi io non avrebbe senso perchè non impareresti u.u
Non ne avrei idea .__.
Non so come quei comandi funzionano nel dettaglio e saprei solo come arrivare al 19 xP

*edit*
doppiavvutieffe O.o
Ho fatto un test:
Code:
int main()
{
	string a,b;
	int x=1;
	b=" hue";
	while(1)
	{
			
		a=x + b; 
		cout<<a<<endl;
		x++;
		if(x>200)
		{
			break;
		}
	}
}
In teoria,mi dovrebbe fare:
Code:
1 hue
2 hue
3 hue
4 hue
5 hue
...
200 hue
E invece,inizia da 'c' e da lì fa' tutto l'ascii,quando arriva al codice 7,il PC emette un bip,il carattere scompare e il programma si impalla,spammando bip finchè non chiudo il processo.
Ma cosa caz...
How about speaking english?
Else other ppl won't help you
Quote Originally Posted by IV2B View Post
Non ne avrei idea .__.
Non so come quei comandi funzionano nel dettaglio e saprei solo come arrivare al 19 xP

*edit*
doppiavvutieffe O.o
Ho fatto un test:
Code:
int main()
{
	string a,b;
	int x=1;
	b=" hue";
	while(1)
	{
			
		a=x + b; 
		cout<<a<<endl;
		x++;
		if(x>200)
		{
			break;
		}
	}
}
In teoria,mi dovrebbe fare:
Code:
1 hue
2 hue
3 hue
4 hue
5 hue
...
200 hue
E invece,inizia da 'c' e da lì fa' tutto l'ascii,quando arriva al codice 7,il PC emette un bip,il carattere scompare e il programma si impalla,spammando bip finchè non chiudo il processo.
Ma cosa caz...
devi convertire il numero in char usando quindi il (char)

Quote Originally Posted by Biesi View Post
How about speaking english?
Else other ppl won't help you
It's true, but if we speak our lenguage, we can understand each other better.
Quote Originally Posted by Biesi View Post
How about speaking english?
Else other ppl won't help you
You're right,however if someone asks you something on the forum in dutch,you'd feel weird answering in english,wouldn't you?

What i'm trying to achieve is having a program that given a X value,creates X number of files on the desktop.
Issue here is that with Sixx93's first answer,i could only make up to 155 files and those files would use ascii names instead than numbers (which is kinda fine anyway,but numbers are preferred).



I'd just need a working code of it,then i'd take care myself of actually understanding how each line works in detail - or - i'll just wait until it's done in classroom.

about the post #5:
i've been doing a test,that code instead of outputting:
1 hue
2 hue
...
200 hue

spams 1 character beginning from 'c' and on character 7 of the ascii code it emits a bip.
Also weird is that it doesn't close unless killing the process,awkward!
Quote Originally Posted by Sixx93 View Post
It's true, but if we speak our lenguage, we can understand each other better.
PM/VM/Skype/MSN
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	int c;
	cout << "Number of files to create: ";
	cin >> c;
	for(int i = 0; i < c; i++)
	{
		ofstream myfile;
		myfile.open(to_string(i) + string(".txt"));

		//WRITE TO FILE

		myfile.close();
	}
}
Quote Originally Posted by Biesi View Post
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	int c;
	cout << "Number of files to create: ";
	cin >> c;
	for(int i = 0; i < c; i++)
	{
		ofstream myfile;
		myfile.open(to_string(i) + string(".txt"));

		//WRITE TO FILE

		myfile.close();
	}
}
IDK why but it says " 'to_string' was not declared in this scope " O.o
Also,what should i change to have the file being outputted on the desktop instead then in the current program's folder? :3
Quote Originally Posted by IV2B View Post
Also,what should i change to have the file being outputted on the desktop instead then in the current program's folder? :3
You'll need the absulte path instead of the relative
Quote Originally Posted by Biesi View Post


You'll need the absulte path instead of the relative
But still the first issue doesn't allow me to actually compile the source *flips table*
Quote Originally Posted by Biesi View Post
myfile.open(to_string(i) + string(".txt"));
I think that you cannot use the '+' to build a string as in vb.net...

Well, I've always failed trying do that.
Quote Originally Posted by Sixx93 View Post
I think that you cannot use the '+' to build a string as in vb.net...

Well, I've always failed trying do that.
Code:
#include <string>
It's tested and working
Posts 1–15 of 19 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us