C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 02-01-2005, 04:56 PM   #1
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
Text file question

Ive got a text file thats a script for AA. Its basically looks like this:

Quote:
admin forceclass 1 m
admin forceclass 2 m
admin forceclass 3 m
admin forceclass 4 m
This means you change the weapon for player 1, 2, 3, .....

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   Reply With Quote
Old 02-01-2005, 05:11 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 02-01-2005, 05:16 PM   #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   Reply With Quote
Old 02-01-2005, 05:31 PM   #4
RoD
Banned
 
RoD's Avatar
 
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   Reply With Quote
Old 02-01-2005, 05:37 PM   #5
RoD
Banned
 
RoD's Avatar
 
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   Reply With Quote
Old 02-01-2005, 05:47 PM   #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   Reply With Quote
Old 02-02-2005, 03:09 PM   #7
RoD
Banned
 
RoD's Avatar
 
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   Reply With Quote
Old 02-02-2005, 03:29 PM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
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
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 02-02-2005, 05:35 PM   #9
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
You must have seen the note i left you w/ pos rep salem : )
RoD is offline   Reply With Quote
Old 02-02-2005, 08:06 PM   #10
RoD
Banned
 
RoD's Avatar
 
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   Reply With Quote
Old 02-02-2005, 08:28 PM   #11
Carnivore ('-'v)
 
Hunter2's Avatar
 
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;
RoD, I thought you were capable of doing such menial tasks yourself...
__________________
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   Reply With Quote
Old 02-03-2005, 02:20 AM   #12
RoD
Banned
 
RoD's Avatar
 
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   Reply With Quote
Old 02-03-2005, 07:53 AM   #13
and the hat of Jobseeking
 
Salem's Avatar
 
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";}'
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 02-03-2005, 03:31 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:01 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22