Thread: Text file question

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Text file question

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

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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;
    }
    Last edited by no-one; 02-01-2005 at 05:18 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    wow thanks man. you know its been too long since you straight did c++ when u dont think to write something. Thnx again.

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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.

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Ok i follow now, thanks alot man i appreciate it. Saved me hours of work!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    You must have seen the note i left you w/ pos rep salem : )

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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....

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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...
    Last edited by Hunter2; 02-02-2005 at 08:33 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    im more into opengl and stuff on that end. i have minimal experience in opening/manip files and stuff honestly.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656

    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Last time I checked, for loops are good at this kinda thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Text File Input Question
    By Ruggles in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 02:18 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM