CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    102

    Inserting numbers from a .txt file into an array?

    Hey all, I'm pretty much a newbie at this kind of thing so go easy on me

    I'm writing a program that takes values from a .txt file and inserts them as input into array elements.

    For example, within the .txt file is "244900422900"

    Using an ifstream object, is there a way to read the first "900" only (ignoring the 244) and input that into the first element of my duration array below?

    BEEP (400, duration[durationCount])

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Inserting numbers from a .txt file into an array?

    To move to a specific place, use seekg. With that and a counter, you will be able to read just the first "900". Probably, you will have to cast it to an int.

    Other way: ignore.
    Last edited by Tronfi; March 22nd, 2009 at 05:32 PM.

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Inserting numbers from a .txt file into an array?

    Quote Originally Posted by Tronfi View Post
    Code:
    ifstream f("myfiletext.txt");
    char a;
    int i=0;
    while(f>>a){
        myarray[i]=a;
        i++;
    }
    I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
    Clarification: Did you want to skip the first 3 characters, whatever they may be, or did you want to find the first occurrence of 244, then take the next 3 characters?
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  4. #4
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Inserting numbers from a .txt file into an array?

    Quote Originally Posted by Etherous View Post
    I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
    I realised what i've written after post it. I edit it quickly, but you were faster . You're totally right.

  5. #5
    Join Date
    May 2006
    Posts
    102

    Re: Inserting numbers from a .txt file into an array?

    Quote Originally Posted by Etherous View Post
    I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
    Clarification: Did you want to skip the first 3 characters, whatever they may be, or did you want to find the first occurrence of 244, then take the next 3 characters?
    The former, that is, I'm looking for something that will skip the first three characters (whatever they may be) and input the next three after that (whatever they may be).

  6. #6
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Inserting numbers from a .txt file into an array?

    Quote Originally Posted by Ulnarian View Post
    The former, that is, I'm looking for something that will skip the first three characters (whatever they may be) and input the next three after that (whatever they may be).
    In that case, this would probably be fastest:
    Code:
    // Variables
    ifstream file(filename,ios::in);
    char instr[7];
    // Get first 6 characters "244900"
    file.read(instr,6);
    // We're done with the file now?
    file.close();
    // Make string valid, by putting end point
    instr[6] = '\0';
    // Convert the string starting from '9' to an integer and set the first element of duration to that number
    duration[0] = atoi(instr[3]);
    Is this what you wanted?
    Last edited by Etherous; March 22nd, 2009 at 05:52 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  7. #7
    Join Date
    May 2006
    Posts
    102

    Re: Inserting numbers from a .txt file into an array?

    Quote Originally Posted by Etherous View Post
    In that case, this would probably be fastest:
    Code:
    ifstream file(filename,ios::in);
    char* instr[7];
    file.read(instr,6);
    file.close();
    instr[6] = '\0';
    duration[0] = atoi(instr[3]);
    Is this what you wanted?
    Possibly, but I'll need some time to digest that (I'm pretty newbish at this programming stuff)

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured