Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading from a text file

reading from a text file

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FredrickNorge
    wrote on last edited by
    #1

    Hi, I have some code which loads a char[] into memory from the source txt file, Example ifstream inn; char text[20]; char text2[10]; inn.open("test.txt"); inn >> text; inn >> text2; cout << text; cout << text2; inn.close(); the text file contains test and test test My problem is that this code only loads each word, seperated by a space in order. I want it to load it in order based on a new line. Any ideas?, thanks Fred

    Z 1 Reply Last reply
    0
    • F FredrickNorge

      Hi, I have some code which loads a char[] into memory from the source txt file, Example ifstream inn; char text[20]; char text2[10]; inn.open("test.txt"); inn >> text; inn >> text2; cout << text; cout << text2; inn.close(); the text file contains test and test test My problem is that this code only loads each word, seperated by a space in order. I want it to load it in order based on a new line. Any ideas?, thanks Fred

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      FredrickNorge wrote:

      My problem is that this code only loads each word, seperated by a space in order. I want it to load it in order based on a new line.

      The opeartor>> will stop when it gets to any whitespace. To get an entire line, use getline instead.

      ifstream inn;
      
      char text[20] = {0};
      char text2[10] = {0};
      inn.open("test.txt");
      inn.getline(text, 19); 
      inn.getline(text2, 19);
      cout << text; cout << text2;
      
      inn.close();
      

      Alternatively, you can use another version of getline that takes a string argument:

      ifstream inn;
      
      string text = "";
      string text2 = "";
      inn.open("test.txt");
      getline(inn, text); 
      getline(inn, text2);
      cout << text.c_str(); cout << text2.c_str();
      
      inn.close();
      

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      F 1 Reply Last reply
      0
      • Z Zac Howland

        FredrickNorge wrote:

        My problem is that this code only loads each word, seperated by a space in order. I want it to load it in order based on a new line.

        The opeartor>> will stop when it gets to any whitespace. To get an entire line, use getline instead.

        ifstream inn;
        
        char text[20] = {0};
        char text2[10] = {0};
        inn.open("test.txt");
        inn.getline(text, 19); 
        inn.getline(text2, 19);
        cout << text; cout << text2;
        
        inn.close();
        

        Alternatively, you can use another version of getline that takes a string argument:

        ifstream inn;
        
        string text = "";
        string text2 = "";
        inn.open("test.txt");
        getline(inn, text); 
        getline(inn, text2);
        cout << text.c_str(); cout << text2.c_str();
        
        inn.close();
        

        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

        F Offline
        F Offline
        FredrickNorge
        wrote on last edited by
        #3

        thanks!, exactly what i needed. Fred

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups