need help with ReadFile need to read in from : to -

This is a discussion on need help with ReadFile need to read in from : to - within the C++ Programming forums, part of the General Programming Boards category; Code: #include "stdafx.h" #include "windows.h" #include <iostream> using namespace std; HANDLE File; char Buf[128]; char lpbuffer[128]; DWORD dwRead; int found ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    317

    need help with ReadFile need to read in from : to -

    Code:
    #include "stdafx.h"
    #include "windows.h"
    #include <iostream>
    using namespace std;
    
    HANDLE File;
    char Buf[128];
    char lpbuffer[128];
    DWORD dwRead;
    
    int found = 0;
    int spaces = 0;
    int j = 0;
    void check();
    void Makeip();
    //: 219.128.254.40 - 
    int _tmain(int argc, _TCHAR* argv[])
    {
    Buf[127] = '\0';
    File = CreateFile("file.txt",GENERIC_READ, 0, NULL ,OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL);
        
    while(1)
    {
    
    ReadFile(File , Buf , sizeof(Buf), &dwRead , NULL);
    for(int i = 0; i < sizeof(Buf); i++) //checks through the lenght of the buffer
    {
    
    	if(Buf[i] == ':') //theres 2 of these , the txt where i want is after the second :
    	{
                    found++; 
                    if(found == 2)
                                         {
                                         //now the text i want to get from the buffer is between : and - but sometimes is in a diferent place on diferent lines so the Buffer array element wont always be in there...
    
                                         }
                    }
    }
    
    }
    return 0;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Anddos View Post
    //now the text i want to get from the buffer is between : and - but sometimes is in a diferent place on diferent lines so the Buffer array element wont always be in there...
    when you have found the second ':' you will have to append the following data to another buffer ( result ) until you reach the '-':.
    Kurt

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Apart from the crap indentation, why aren't you using standard stream I/O and standard strings for this?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    what do you mean make another
    ReadFile(File , Buf , sizeof(Buf), &dwRead , NULL);

    but wont that start on the first line at the beginging ?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I was thinking about something like this
    Code:
    char result[100];
    char * resp = result;
    while(1) {
        ReadFile(File , Buf , sizeof(Buf), &dwRead , NULL);
        for(int i = 0; i < sizeof(Buf); i++) { //checks through the lenght of the buffer
            if(Buf[i] == ':') //theres 2 of these , the txt where i want is after the second :
                found++; 
            if(found == 2) {
                if ( Buf[i] != '-' ) 
                    *resp++ = Buf[i];
                else {
                    *resp = 0;
                }
            }
        }
    }
    skipping the first char, range checking and breaking out of the loop is left as an exercise.
    Kurt
    Last edited by ZuK; 04-11-2007 at 12:05 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    found has to be reset to 0 so it works again for the next line

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Anddos View Post
    found has to be reset to 0 so it works again for the next line
    I wonder what you are talking about. You never check for a linebreak in this code.
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    the text that i need to grab is in like this

    3.8 LME: wordshere - checked , between : and -
    dont need anything else copied after it

    oh a line break is like '\n' right?

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    Buf[127] = '\0';
    File = CreateFile("part1.txt",GENERIC_READ, 0, NULL ,OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL);
        
    
    	
    
    char *result[15];
    
    
    s:
        ReadFile(File , Buf , sizeof(Buf), &dwRead , NULL);
    //	cout << Buf;
    	Sleep(50);
    	for(int i = 0; i < strlen(Buf); i++)
    	{
    		if(Buf[i] == ':')
    		{
    		     found++;
    			 if(found == 2)
    			 {
    				for(int j = 2; j < 15; j++)
    				{
    					if(Buf[i+j] == '-' || Buf[i+j] == 'V')
    					{
    						break;
    					}
    					else
    					{
    					cout << Buf[i+j];
    					sprintf(result[k++] , "&#37;c", Buf[i+j]); //stuck on this part <-------------------
    					}
    				}
    			 }
    		}
    	
    	}
    
    	cout << result << endl;
    	found = 0;
    	goto s;
    
    
    
    return 0;
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    This just goes from bad to worse, now it has goto's to implement a while loop.

    First question still stands.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:52 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21