Author |
Message |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Fri Apr 04, 2008 10:09 pm Post maybe stupid Post subject: funster code help |
 |
|
|
|
well I am trying to genorate a random file name and ofstream that file.. this is what i got so far
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
#include <cstdlib>
#include <time.h>
#include <sstream> // needed to convert int to string
using namespace std;
const int LOW = 1;
const int HIGH = 1000;
void randomfilename(string a, string b, string c, string d, string e)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int random = rand() % (HIGH - LOW + 1) + LOW;
string s;
std::stringstream out;
out << random;
s = out.str();
ofstream goods;
goods.open(a+s+".txt");
goods<<random;
goods.close();
}
int main()
{
randomfilename("hi","hi","hi","hi","hi");
return 0;
}
| and it compiles with those errors
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Owner\Desktop\Programing\c++\VIRUSES\360 mod\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Owner\Desktop\Programing\c++\VIRUSES\360 mod\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
main.cpp: In function `void randomfilename(std::string, std::string, std::string, std::string, std::string)':
main.cpp:36: error: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/Dev-Cpp/include/c++/3.4.2/fstream:695: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
make.exe: *** [main.o] Error 1
Execution terminated |
|
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Fri Apr 04, 2008 11:23 pm Post maybe stupid Post subject: |
 |
|
|
|
Lame. Use sprintf. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Sat Apr 05, 2008 12:04 am Post maybe stupid Post subject: |
 |
|
|
|
I thought about it. If I was gonna use sprintf I would have used snprintf, but depending on which compiler he's using it might be snprintf or _snprintf, which was not worth the trouble of explaining. |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sat Apr 05, 2008 10:29 am Post maybe stupid Post subject: |
 |
|
|
|
For a situation like this, sprintf is ok, since you're not using user input (or strings at all) as part of the print. |
|
Back to top |
|
 |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Sat Apr 05, 2008 6:24 pm Post maybe stupid Post subject: |
 |
|
|
|
wait can you explain this 'a' + rand() % 26; ? (i know creates random name but how?) |
|
Back to top |
|
 |
k0zy Server Help Squatter

Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
Posted: Sat Apr 05, 2008 8:09 pm Post maybe stupid Post subject: |
 |
|
|
|
Characters aren't really stored as characters, but as their equivalent ascii code.
So what it does is, it takes the ascii code of the letter a and adds a number between 0 and 26 to it.
This results is a new ascii code, which can be interpreted as a character. _________________ It's a shark! Oh my god! Unbelievable! |
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Sat Apr 05, 2008 8:56 pm Post maybe stupid Post subject: |
 |
|
|
|
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote: | Characters aren't really stored as characters, but as their equivalent ascii code.
So what it does is, it takes the ascii code of the letter a and adds a number between 0 and 26 to it.
This results is a new ascii code, which can be interpreted as a character. | Between 0 and 25 actually... but yeah, pretty much right
Bob Dole lol... _________________ (Insert a bunch of dead links here) |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Sun Apr 06, 2008 1:05 pm Post maybe stupid Post subject: |
 |
|
|
|
oh this isnt complicated at all, cuz first time i saw that didnt get anything :\ |
|
Back to top |
|
 |
tcsoccerman Server Help Squatter
Age:33 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Sun Apr 06, 2008 5:40 pm Post maybe stupid Post subject: |
 |
|
|
|
which is a reason to use number values (int) instead of char values if possible to save memory  |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Sun Apr 06, 2008 6:02 pm Post maybe stupid Post subject: |
 |
|
|
|
where's the talking out of ass smiley?
sizeof(char) < sizeof(int) |
|
Back to top |
|
 |
tcsoccerman Server Help Squatter
Age:33 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Sun Apr 06, 2008 6:13 pm Post maybe stupid Post subject: |
 |
|
|
|
really? i read that somewhere?!?! |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Sun Apr 06, 2008 6:40 pm Post maybe stupid Post subject: |
 |
|
|
|
don't believe everything you read then? _________________ Performance is often the art of cheating carefully. - James Gosling |
|
Back to top |
|
 |
tcsoccerman Server Help Squatter
Age:33 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Sun Apr 06, 2008 6:45 pm Post maybe stupid Post subject: |
 |
|
|
|
touche. i dunno it said that char's get turned into int values or something. |
|
Back to top |
|
 |
Animate Dreams Gotta buy them all! (Consumer whore)

Age:37 Gender: Joined: May 01 2004 Posts: 821 Location: Middle Tennessee Offline
|
Posted: Mon Apr 14, 2008 10:12 pm Post maybe stupid Post subject: |
 |
|
|
|
I think the idea is that an int can store the number 4 000 000 000 in 4 bytes, whereas 4 000 000 000 in a char array takes up 10 chars, and therefore 10 bytes. |
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Mon Apr 14, 2008 11:34 pm Post maybe stupid Post subject: |
 |
|
|
|
... you never store numbers as characters anyway...
What tcsoccerman is talking about is probably that any char value is actually a number between 0 and 255, and you can add 'a' to 800, for example, and it would give you 897 or something |
|
Back to top |
|
 |
tcsoccerman Server Help Squatter
Age:33 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Tue Apr 15, 2008 9:36 pm Post maybe stupid Post subject: |
 |
|
|
|
yeh pretty much. |
|
Back to top |
|
 |
|