Server Help

Trash Talk - funster code help

hellzlaker - Fri Apr 04, 2008 10:09 pm
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
Code: Show/Hide

#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

Code: Show/Hide
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

Bak - Fri Apr 04, 2008 10:15 pm
Post subject:
Code: Show/Hide

#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main()
{
  int namelength = 10;
  char buf[2] = {0, 0};
  string name;

  srand((int)time(0));

  for (int x = 0; x < namelength; ++x)
  {
    buf[0] = 'a' + rand() % 26;
    name += buf;
  }

  name += ".txt";

  ofstream fout(name.c_str());

  if (fout.good())
    fout.close();

  return 0;
}


Dr Brain - Fri Apr 04, 2008 11:23 pm
Post subject:
Lame. Use sprintf.
Bak - Sat Apr 05, 2008 12:04 am
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.
Dr Brain - Sat Apr 05, 2008 10:29 am
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.
hellzlaker - Sat Apr 05, 2008 6:24 pm
Post subject:
wait can you explain this 'a' + rand() % 26; ? (i know creates random name but how?)
k0zy - Sat Apr 05, 2008 8:09 pm
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.
Samapico - Sat Apr 05, 2008 8:56 pm
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 tongue.gif
Bob Dole lol...
Bak - Sun Apr 06, 2008 2:33 am
Post subject:
http://www.asciitable.com/

if you look there you see that 'a' is equal to 97 (decimel) so you can do

Code: Show/Hide
if ('a' == 97)
   cout << "it's true!" << endl;
else
   cout << "lies!" << endl;


and it'll show you that it's true. So % is the modulo operator which is basically remainder after you divide. So if I divide rand() (which is a number between zero and 32767) by 26 the remainder will be between 0 and 25. Thus I will get number between 97 and 122, which are the letters 'a' to 'z'.
hellzlaker - Sun Apr 06, 2008 1:05 pm
Post subject:
oh this isnt complicated at all, cuz first time i saw that didnt get anything :\
tcsoccerman - Sun Apr 06, 2008 5:40 pm
Post subject:
which is a reason to use number values (int) instead of char values if possible to save memory icon_smile.gif
Bak - Sun Apr 06, 2008 6:02 pm
Post subject:
ac_withstupid.gif

where's the talking out of ass smiley?

sizeof(char) < sizeof(int)
tcsoccerman - Sun Apr 06, 2008 6:13 pm
Post subject:
really? i read that somewhere?!?!
CypherJF - Sun Apr 06, 2008 6:40 pm
Post subject:
don't believe everything you read then?
tcsoccerman - Sun Apr 06, 2008 6:45 pm
Post subject:
touche. i dunno it said that char's get turned into int values or something.
Animate Dreams - Mon Apr 14, 2008 10:12 pm
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.
Samapico - Mon Apr 14, 2008 11:34 pm
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
tcsoccerman - Tue Apr 15, 2008 9:36 pm
Post subject:
yeh pretty much.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group