Results 1 to 8 of 8

Thread: Read Lines from a file and split each one

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    Greece
    Posts
    12

    Read Lines from a file and split each one

    Hi

    I am new to C++ and I want to write a programm that reads lines from a file into character arrays and then splits them (the arrays) into two parts. The first part is before the delimeter (usually " ") and the second after it. The file is consisted of eleven lines. The first is a number that must be stored in a k variable. The 10 remaining have 2 numbers each and the numbers are divided by a space (" ")
    e.g
    ---Start of file-----
    5
    2 12
    3 45
    1 20
    5 34
    7 29
    8 84
    10 28
    9 14
    ---End of file-----

    Total eleven lines

    Here's how I think the code should be. Please help me fill in the missing parts of the loop


    int k;
    ifstream b_file("input.txt");
    b_file>>k;
    //The above part reads the first number and stores it in k var


    for (i=1; i<=10; i++)
    {
    //The code in here must input Line of file into Line[i] string
    //the code in here must split Line[i] into TempA[i] (this is the first number of the line) and TempB[i] (this is the second number of the line) with " " as the delimeter
    }
    b_file.close();


    Thanks for your help

    Wh1t3w0lf

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm not familiar with the c++ file streams. If you don't need the string really, it is easy:

    Code:
    for (i=1; i<=10; i++) 
    { 
       b_file >> TempA[i] >> TempB[i];
    }
    But if you really need the string it gets trickier:
    Code:
    char Line[10][10];  // 10 strings of 10 characters each
    char* secNum;  // pointer to the second number
    
    for (i=1; i<=10; i++) 
    { 
    //The code in here must input Line of file into Line[i] string 
      b_file >> Line[i];
    // the code in here must split Line[i] into TempA[i] (this is the first // number of the line) and TempB[i] (this is the second number of // the line) with " " as the delimeter 
      TempA[i] = atoi(Line[i]);  // first number
      secNum = strtok(Line[i], " ");  // find space
      TempB[i] = atoi(secNum);  // second number
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    Greece
    Posts
    12

    Re

    I tested this:

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>


    main()
    {
    char Line[10][10]; // 10 strings of 10 characters each
    char* secNum; // pointer to the second number

    int k;
    ifstream b_file("input.txt");
    b_file>>k;
    int i;
    char TempA[10];
    char TempB[10];

    for (i=1; i<=10; i++)
    {

    b_file >> Line[i];
    TempA[i] = atoi(Line[i]); // first number
    secNum = strtok(Line[i], " "); // find space
    TempB[i] = atoi(secNum); // second number
    cout<<"First Number: "<< TempA[i] <<" second number: "<<TempB[i];
    }
    b_file.close();

    return 0;
    }


    And the output I get is wrong. It keeps printing various characters instead of the ones that it should print.
    Also I need the two numbers of each line (I don't just want to print them- I did it as a test to see if it works)

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    char is interpreted by iostreams as a character, cast them to an int before inserting them:
    Code:
    cout<<"First Number: "<< (int)TempA[i] <<" second number: "<< (int)TempB[i];
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    Greece
    Posts
    12
    None of these workes. Anyway I sat and wrote a custom made routine for this $#^% Here it is

    ifstream b_file("input.txt");
    b_file>>k;

    for (i=1; i<=20; i++)
    b_file >> number[i];
    for (i=1; i<=10; i++)
    {
    first[i]=atoi(number[2*i-1]);
    second[i]=atoi(number[2*i]);
    }
    b_file.close();


    So now we have the 10 first numbers in first[i] (i 1 to 10)
    and the 10 second number in second[i] (i 1 to 10)


    I want to thank you guys for helping me.

    Wh1tew0lf

    P.S By the way strings in C++ SUCK!!!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by Wh1t3w0lf
    P.S By the way strings in C++ SUCK!!!
    You mean C

    Neither C or C++ has intrinsic string handling capability - it's not part of the design, since they introduce inefficiencies. However, C++ has the string class as part of the Standard Library, which you should (to be correct) use instead of character arrays.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, I'm sorry if my functions didn't work (did you try the first one? Judging from your own solution you wouldn't need the extended features of the second.), but in any case, make sure that you start the array indizes at 0, not at 1 like in VB. You'll a) waste memory and b) will get weird results (access violations etc.) when you try to make a 20 integer array (int ar[20]) and then want to access the last member (ar[19] would be correct, but ar[20] would be the VB way).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    Greece
    Posts
    12
    damn...didn't know about the string class

    CornedBee your functions are proly correct but I'm such a n00b (or lamo) and I can't make them work.... Anyway thanks for your help.

    Wh1t3w0lf

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