![]() |
| | #1 | |
| Banned Join Date: Sep 2002
Posts: 6,334
| Text file question Quote:
Now for server rotation reasons i need to add a line for players 1-1000.... Ive done up to 22 because our server is a 22 player slot, but this only works if the rotation hasnt placed it up over 22. Anyway, is there a way i could "spam" 980 lines into the file, and have them still be in order? IE i could copy/paste lines but id still have to go through and change each player number.....in 15 diff text files LOL. Thats alot of time.....so is there a way? | |
| RoD is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| Use PERL It's great for all this text file generating, especially where there's any kind of pattern to it. |
| Salem is offline | |
| | #3 |
| Has a Masters in B.S. Join Date: Aug 2001
Posts: 2,267
| you could extend something like this? Code:
#include <stdio.h>
#define STARTPLAYER 22
#define NPLAYERS 1000
int main(int argc, char** argv)
{
FILE* fp = NULL;
if(argc <= 1)
{
printf("no file name provided");
return 0;
}
fp = fopen(argv[1],"a");
if(!fp)
{
printf("Unable to open file %s", argv[1]);
return 0;
}
for(int i = STARTPLAYER;i <= NPLAYERS; ++i)
{
fprintf(fp,"admin forceclass %d m\n",i);
}
fclose(fp);
return 0;
}
__________________ ADVISORY: This users posts are rated CP-MA, for Mature Audiences only. Last edited by no-one; 02-01-2005 at 05:18 PM. |
| no-one is offline | |
| | #4 |
| Banned Join Date: Sep 2002
Posts: 6,334
| wow thanks man. you know its been too long since you straight did c++ when u dont think to write something. Thnx again. |
| RoD is offline | |
| | #5 |
| Banned Join Date: Sep 2002
Posts: 6,334
| dumb question, as ive never worked with this end of C++, say i have text file test in the folder with the project, how do i tell the program to write to that partic file....hard to follow code i dont know lol. |
| RoD is offline | |
| | #6 |
| Has a Masters in B.S. Join Date: Aug 2001
Posts: 2,267
| it may be less familiar since its all C, i tend to find C more intuitive and simpler than C++. "generally" if you just want it to look in the folder the program is executing in open the file with no drives or folders specified(ie. "test.txt" as the specified filename) on Windows anyway, or hardcoded its like this. Code: #include <stdio.h>
#define STARTPLAYER 22
#define NPLAYERS 1000
#define FILENAME "test.txt"
int main(void)
{
FILE* fp = fopen(FILENAME,"a");
if(!fp)
{
printf("Unable to open file %s", FILENAME);
return 0;
}
for(int i = STARTPLAYER;i <= NPLAYERS; ++i)
{
fprintf(fp,"admin forceclass %d m\n",i);
}
fclose(fp);
return 0;
}
__________________ ADVISORY: This users posts are rated CP-MA, for Mature Audiences only. |
| no-one is offline | |
| | #7 |
| Banned Join Date: Sep 2002
Posts: 6,334
| Ok i follow now, thanks alot man i appreciate it. Saved me hours of work! |
| RoD is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| Heh Code: $ perl -e'open(FH,">blah.txt") or die "oops";
for(my $i=22;$i<33;$i++) { print FH "admin forceclass $i m\n"; }'
$ cat blah.txt
admin forceclass 22 m
admin forceclass 23 m
admin forceclass 24 m
admin forceclass 25 m
admin forceclass 26 m
admin forceclass 27 m
admin forceclass 28 m
admin forceclass 29 m
admin forceclass 30 m
admin forceclass 31 m
admin forceclass 32 m
|
| Salem is offline | |
| | #9 |
| Banned Join Date: Sep 2002
Posts: 6,334
| You must have seen the note i left you w/ pos rep salem : ) |
| RoD is offline | |
| | #10 |
| Banned Join Date: Sep 2002
Posts: 6,334
| one more question: Lets say i want to be random about it. Example 1 m9 2 rpk 3 m4m 4 rpk 5 m9 etc....or just two back and forth 1 rpk 2 m4m 3 rpk 4 m4m ...i would need to adjust to a for loop, but im not used to C and writing to files.... |
| RoD is offline | |
| | #11 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| Hm.. Code: #include (required headers)
int rnd(int range)
{
return (int)(((double)rand() / (double)(RAND_MAX + 1)) * range);
}
int main()
{
srand(time(0));
std::vector<std::string> weaps;
weaps.push_back("rpk");
weaps.push_back("m4m");
weaps.push_back("m9");
std::ofstream file("test.txt");
for(int i = 1; i <= 980; ++i)
file << i << ' ' << weaps[rnd(weaps.size())] << '\n';
file << std::flush;
return 0;
}
Code: std::string weaps[2];
weaps[0] = "m4m";
weaps[1] = "rpk";
std::ofstream file("test.txt");
for(int i = 1; i <= 980; ++i)
file << i << ' ' << weaps[i % 2] << '\n';
file << std::flush;
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. Last edited by Hunter2; 02-02-2005 at 08:33 PM. |
| Hunter2 is offline | |
| | #12 |
| Banned Join Date: Sep 2002
Posts: 6,334
| im more into opengl and stuff on that end. i have minimal experience in opening/manip files and stuff honestly. |
| RoD is offline | |
| | #13 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| ![]() Code: $ perl -e'open(FH,">blah.txt") or die "oops";
my @foo=qw(m9 rpk m4m wewd cffs kfjk);
for(my $i=1;$i<=10;$i++){print FH "$i $foo[rand($#foo+1)]\n";}'
|
| Salem is offline | |
| | #14 |
| People Love Me Join Date: Jan 2003 Location: Ohio
Posts: 412
| Last time I checked, for loops are good at this kinda thing. |
| Krak is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginner Text File Input Question | Ruggles | C++ Programming | 2 | 11-12-2006 02:18 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Batch file programming | year2038bug | Tech Board | 10 | 09-05-2005 03:30 PM |
| Read word from text file (It is an essay) | forfor | C Programming | 7 | 05-08-2003 11:45 AM |
| Ok, Structs, I need help I am not familiar with them | incognito | C++ Programming | 7 | 06-29-2002 09:45 PM |