Thread: Retrieve string(s) from (txt) file

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Retrieve string(s) from (txt) file

    Hi,

    I am now working on a little app which should be able to cooperate with text file. The app. uses list boxes. There is no problem with saving strings from list box to txt file in appropriate order.

    But I have issues with adding strings from the text file to the list box in order as they are typed in the file (Each string is saved on a blank line). I´d suppose each line in txt file would stand for each line in list box... Simple isnt it?

    What have I reached so far is that whole text file is copied to one line of list box, which apparantly does not fullfit its purpose.

    So is there some function which would indicate number of lines in a txt file and which would allow me to operate with single line at a time?

    Thank you very much for your time spent with this thread

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Seems like it is more a problem of how you are reading the strings read from the file and adding them to the list box than figuring out how many lines are in the file. Show the code you have for doing this. Sounds like you should be using getline to read in items from the file followed by whatever listbox control method to add the strings read from the file into the listbox in question.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by hk_mp5kpdw View Post
    Seems like it is more a problem of how you are reading the strings read from the file and adding them to the list box than figuring out how many lines are in the file. Show the code you have for doing this. Sounds like you should be using getline to read in items from the file followed by whatever listbox control method to add the strings read from the file into the listbox in question.
    This is the function which handles reading from txt file and writeing text to list the box

    Code:
    BOOL OnInclude(HWND hwndDlg)
    {
    	TCHAR chEnIncl[1024];
    	HANDLE hFileEn;
    	DWORD dwSize;
    	DWORD dw;
    	LPBYTE lpBuffer = NULL;
    	hFileEn =	CreateFile(TEXT("C:/Program Files/Vocabulary assistant/en.va"), GENERIC_READ, 0,NULL, OPEN_EXISTING, 0, NULL);
    	if ( hFileEn == INVALID_HANDLE_VALUE )
    		return FALSE;
    	dwSize = GetFileSize(hFileEn, NULL);
    	lpBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, dwSize+1);
    	if ( !ReadFile(hFileEn, lpBuffer, dwSize, &dw, NULL) )
    	{
    		CloseHandle(hFileEn);
    		HeapFree(GetProcessHeap(), 0, lpBuffer);
    		return FALSE;
    	}
    	CloseHandle(hFileEn);
    	lpBuffer[dwSize] = NULL;
    	SendDlgItemMessage(hwndDlg, IDC_LISTEN, LB_ADDSTRING, NULL, (LPARAM)lpBuffer); 
    	if ( !HeapFree(GetProcessHeap(), 0, lpBuffer) )
    		return FALSE;
    	return TRUE;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This might be better in the Windows programming section.

    You are reading in the entire file with ReadFile and sending it to the listbox as one big string. I believe the listbox will add a separate entry each time you send the add string message, so since you only send one message it only adds one string.

    You have to find a way to separate each line of text from the file. One possible solution would be to put the buffer in a stringstream and use getline in a loop to get each line and send an add string message with each line's text. You might also be able to read lines indivdually with a windows function instead of ReadFile, although I'm not sure if such a function exists. Finally, you could do the string processing yourself by looking for new lines (or whatever end of line character the file has) and separating the buffer that way.

  5. #5
    A.I Programmer
    Join Date
    Mar 2007
    Location
    Teresina - Brazil
    Posts
    47
    Code:
      
      #include <stdio.h>
    
      /*(...)*/
    
      ListBox1 = CreateWindowEx(WS_EX_CLIENTEDGE,"LISTBOX","",WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_SORT,10,10,180,200,hwnd,NULL,GetModuleHandle(NULL),NULL);
                   
      char *List;
      FILE *Fp;
               
      List = (char*)LocalAlloc(LPTR,256);
               
      Fp = fopen("data.txt","r+");
                  
      while(!feof((FILE *)Fp))
      {
          fscanf(Fp,"%s",List);
          SendMessage(ListBox1,LB_ADDSTRING,0,(LPARAM)List);
      }                     
                 
       fclose(Fp);
       LocalFree((HLOCAL)List);

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Won't that code add the last line in the file twice? What about strings with embedded spaces?

  7. #7
    A.I Programmer
    Join Date
    Mar 2007
    Location
    Teresina - Brazil
    Posts
    47
    Here the last line is added only ONE time...

    Failures about "white spaces" can be solved with this function:

    Code:
    void CharReplace(char *Buffer, char Ch, const char Replace)
    {
       long i;
         
       for(i = 0; Buffer[i] != '\0'; i++)
       {
          if(Buffer[i] == Ch)
          {
             Buffer[i] = Replace;
          }
       }    
    }
    Use this mode:

    Code:
      /*To write in the file:*/
    
      CharReplace(yourtext,' ','§');
    
      /*To read of the file*/
    
      CharReplace(yourtext,'§',' ');

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Daved View Post
    This might be better in the Windows programming section.

    You are reading in the entire file with ReadFile and sending it to the listbox as one big string. I believe the listbox will add a separate entry each time you send the add string message, so since you only send one message it only adds one string.

    You have to find a way to separate each line of text from the file. One possible solution would be to put the buffer in a stringstream and use getline in a loop to get each line and send an add string message with each line's text. You might also be able to read lines indivdually with a windows function instead of ReadFile, although I'm not sure if such a function exists. Finally, you could do the string processing yourself by looking for new lines (or whatever end of line character the file has) and separating the buffer that way.
    Yes that is the sollution I am looking for, but how do I actually do it. For exmaple how does a programme indicate new line character? What function shal I use?

    Anyways thanks for helping me

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Like I said, you can use a stringstream, are you familiar with those? The getline function reads in a single line and it knows where the end of the line is, so you shouldn't have to worry about it if you use getline.

    Another option would be to use a different method for reading in the file. Using fopen is one alternate method (although I wouldn't use fscanf, but maybe fgets instead). Using an ifstream with getline would work as well (and is likely what I would pick).

    Any of these choices will probably work, it just depends on what you know. If you don't know any oft hem, do you know how to search a string and look for '\n' or "\r\n"?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Here the last line is added only ONE time...
    Are you sure? You are checking for eof before you attempt to read, so if the last read reaches end of file you will re-usethe previous line's data. There is an entry in the FAQ about why using feof in this way is bad practice.

    >> Failures about "white spaces" can be solved with this function
    I'm not terribly familiar with fscanf, but won't it's usage as you've shown only read in a whitespace delimited string? Gordon indicated that each line in the file should get its own entry in the list box, but if you read in whitespace delimited words then you might create multiple entries in the listbox for a single line. For example, "Hello World" would be read in as "Hello" and "World", but the OP wants "Hello World" to be added as a single entry.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Daved View Post
    Like I said, you can use a stringstream, are you familiar with those? The getline function reads in a single line and it knows where the end of the line is, so you shouldn't have to worry about it if you use getline.

    Another option would be to use a different method for reading in the file. Using fopen is one alternate method (although I wouldn't use fscanf, but maybe fgets instead). Using an ifstream with getline would work as well (and is likely what I would pick).

    Any of these choices will probably work, it just depends on what you know. If you don't know any oft hem, do you know how to search a string and look for '\n' or "\r\n"?
    This is my first time creating an app which works with txt data, so I have actually no idea about serching strings and characters.

    Which function do you recommend to use in this case?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would open the file with an ifstream, and read each line with getline. There are tons of examples online or in the C++ programming section. I'd use those because that's what I'm familiar with, though.

    It's a pretty simple process. Create an ifstream and open the file. Create a while loop and call getline inside the while control (loop while getline returns "true"). Inside the loop put your add string code to add the string you read in.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Well ok, I´ll take a look at some threads containing this IFSTREAM function and then will see...

    Thx again for support

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM