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. Read string at a certain point?

Read string at a certain point?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 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.
  • A Offline
    A Offline
    Aoife
    wrote on last edited by
    #1

    Hello, I need to read the following two lines from a .txt file. Name John Doe Number +12345 Then I must extract "John Doe" and "+12345" and send them to an edit box. I tried using strtok() but it didn't work the way I want it to. Is there a way of starting at a particular point in the string? eg in "Name John Doe" at position 6. Thanks, Aoife /********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); char *pNext1 = ?????(bufLine1, ??); char *pNext2 = ?????(bufLine2, ??); strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); SendDlgItemMessage(IDC_BOX1, EM_REPLACESEL, FALSE, (LPARAM)bufLine1); SendDlgItemMessage(IDC_BOX2, EM_REPLACESEL, FALSE, (LPARAM)bufLine2); fclose( stream );

    M A T 3 Replies Last reply
    0
    • A Aoife

      Hello, I need to read the following two lines from a .txt file. Name John Doe Number +12345 Then I must extract "John Doe" and "+12345" and send them to an edit box. I tried using strtok() but it didn't work the way I want it to. Is there a way of starting at a particular point in the string? eg in "Name John Doe" at position 6. Thanks, Aoife /********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); char *pNext1 = ?????(bufLine1, ??); char *pNext2 = ?????(bufLine2, ??); strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); SendDlgItemMessage(IDC_BOX1, EM_REPLACESEL, FALSE, (LPARAM)bufLine1); SendDlgItemMessage(IDC_BOX2, EM_REPLACESEL, FALSE, (LPARAM)bufLine2); fclose( stream );

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      Have a look at CString's Mid and Right functions. Michael :-)

      1 Reply Last reply
      0
      • A Aoife

        Hello, I need to read the following two lines from a .txt file. Name John Doe Number +12345 Then I must extract "John Doe" and "+12345" and send them to an edit box. I tried using strtok() but it didn't work the way I want it to. Is there a way of starting at a particular point in the string? eg in "Name John Doe" at position 6. Thanks, Aoife /********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); char *pNext1 = ?????(bufLine1, ??); char *pNext2 = ?????(bufLine2, ??); strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); SendDlgItemMessage(IDC_BOX1, EM_REPLACESEL, FALSE, (LPARAM)bufLine1); SendDlgItemMessage(IDC_BOX2, EM_REPLACESEL, FALSE, (LPARAM)bufLine2); fclose( stream );

        A Offline
        A Offline
        Alexandru Savescu
        wrote on last edited by
        #3

        You can use std::string and std::ifstream like this; ifstream in ("number"); string dummy, Name, Number; in >> dummy >> Name; int >> dummy >> Number; Best regards, Alexandru Savescu

        1 Reply Last reply
        0
        • A Aoife

          Hello, I need to read the following two lines from a .txt file. Name John Doe Number +12345 Then I must extract "John Doe" and "+12345" and send them to an edit box. I tried using strtok() but it didn't work the way I want it to. Is there a way of starting at a particular point in the string? eg in "Name John Doe" at position 6. Thanks, Aoife /********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); char *pNext1 = ?????(bufLine1, ??); char *pNext2 = ?????(bufLine2, ??); strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); SendDlgItemMessage(IDC_BOX1, EM_REPLACESEL, FALSE, (LPARAM)bufLine1); SendDlgItemMessage(IDC_BOX2, EM_REPLACESEL, FALSE, (LPARAM)bufLine2); fclose( stream );

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          If you know that the text starts in column 6, then

          char *pNext1 = &bufLine1 [6];

          will work just fine. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

          A 1 Reply Last reply
          0
          • T Tim Smith

            If you know that the text starts in column 6, then

            char *pNext1 = &bufLine1 [6];

            will work just fine. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            A Offline
            A Offline
            Aoife
            wrote on last edited by
            #5

            This worked perfectly, Thanks!

            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