NO

Author Topic: parsing a file  (Read 5565 times)

datatrap

  • Guest
parsing a file
« on: June 08, 2012, 07:52:25 AM »
Hello fellow humans.
Here is what I am trying to do:
I want to be able to choose the directory of any file on my computer and parse the contents of any file chosen. My question is....How do I view directories other than the file where my .exe resides?
Is it something like

system("c:dir");
Here is what I have so far for opening the file.

                                system("dir");
            do
            { 
            FILE * _popen(const char *string, const char *mode);
            scanf("%s",path);   
            printf("opening file %s\n",path);         
            html=fopen(path,"r");
            if(html==NULL)
            {
            perror("file not loaded\nerror code 1\n");
            }
            }
            while(html==NULL);
            //check size*******************************************************************************************************
            while ((ch = fgetc(html)) != EOF){file_size++;
            }    
            /*counts characters in file*/
            printf("file_size =%i\n",file_size);
                        //close file********************************************************************************************************
            fclose(html);
            printf("\n");
            //open file********************************************************************************************************
            html=fopen(path,"r");              /*opens file*/
            if(html==NULL){perror("file not loaded\nerror code 2\n");
            }
            printf("\n");
            //move to buffer*************************************************************************************************
            char buffer[file_size];    /*saves file to buffer*/
            while ((ch = fgetc(html)) != EOF){buffer
  • =ch;x++;

            }
            printf("\n");
            //close file*********************************************************************************************************
            fclose(html);


Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: parsing a file
« Reply #1 on: June 08, 2012, 09:33:18 AM »
To control the directory you can use:

DWORD GetCurrentDirectory(
    DWORD nBufferLength,   // size, in characters, of directory buffer
    LPTSTR lpBuffer    // address of buffer for current directory
   );   
 

BOOL SetCurrentDirectory(
    LPCTSTR lpPathName    // address of name of new current directory
   );   

I have added a little program of mine, where I delete to the recycle bin. I search and change there the directories. Perhaps it can help you as example.
best regards
 Alex ;)

CommonTater

  • Guest
Re: parsing a file
« Reply #2 on: June 08, 2012, 02:36:15 PM »
With respect for my friend Alex... GetCurrentDirectory() and SetCurrentDirectory() is one way to do this... but they change more than just the name of the directory you're viewing... SetCurrentDirectory() also changes where files are stored if you don't specify a path so it should be used with deliberacy and you should store the original directory name to put it back when you're done.  More than one programmer has gotten a big surprise when writing a file they later cannot find.

If you are looking for a list of all files in a folder... The better plan is to use FindFirstFile() and FindNextFile() from the windows api.  These will allow you to enumerate the files in any folder, find folders etc. without disturbing system settings. And as a bonus, they're going to be 10X faster than system() calls. 
 
If you already know the path and name of the file and just need to check it's size (etc.) you should use GetFileAttributesEx().
 
If all you need is a quick check to see if a given file exists or not you can use GetFileAttributes() which returns a special value if the file doesn't exist.
 
Also...
There are better ways to load a file into memory than getch()
Making large file buffers on the stack is begging for a program crash, you should use malloc() instead.
and
You should know that scanf() will stop on the first space it encounters...
 
Try something like this...
Code: [Select]
#include <windows.h>
#include <stdio.h>
 
char fname[MAX_PATH];  // user's file name
unsigned char* data;      // file contents in memory
WIN32_FILE_ATTRIBUTE_DATA fad;   // data for getfileattributesex
FILE* fh;   // file handle
 
 
// get filename from user and other stuff...
 
//check file and get size
if ( ! GetFileAttributesEx( fname, GetFileExInfoStandard, &fad) )
  {  // file not found, handle error }
 
// open the file
fh = fopen(fname, "rb");  // open in binary mode
if ( ! fh )
  { // file not open, handle error }
 
// reserve memory for the file
data = malloc( fad.nfilesizelow + 5);  // always get a little extra
 
// load into memory
fread(data, fad.nfilesizelow, 1, fh);
 
// close the file
fclose (fh);
 
 
// do what you gotta do with the file data
 
free(data);

Hope this helps....
 
 
« Last Edit: June 08, 2012, 03:32:02 PM by CommonTater »

datatrap

  • Guest
Re: parsing a file
« Reply #3 on: June 09, 2012, 07:03:38 AM »
Wow thanks for the information. That will give me something to chew on for a while. I knew there would be some better ways to do what I have rigged together. I just love writing code.
Here is the parsing section of the algorithm I put together......
                 for(x=start_of_file;x<=end_of_file;x++)
      {
      A=tag_length-1;     //tag_length is the size of the phrase you are looking for
      while(buffer[x+A]==element[A])
      {
      A--;         
      }   
      if(A==-1)
      {   
                 printf("here is where I found the element you are looking for %i",x);      
      }
       

CommonTater

  • Guest
Re: parsing a file
« Reply #4 on: June 09, 2012, 07:42:02 AM »
Wow thanks for the information. That will give me something to chew on for a while. I knew there would be some better ways to do what I have rigged together. I just love writing code.
Here is the parsing section of the algorithm I put together......
                 for(x=start_of_file;x<=end_of_file;x++)
      {
      A=tag_length-1;     //tag_length is the size of the phrase you are looking for
      while(buffer[x+A]==element[A])
      {
      A--;         
      }   
      if(A==-1)
      {   
                 printf("here is where I found the element you are looking for %i",x);     
      }
       


If you've got the whole thing in memory as a continuous buffer... you can treat it as a string and use string search tools from the CRT libraries...
Code: [Select]
char* ptr =  strstr(Buffer, Phrase);
Buffer can be any text in memory.  Phrase can be anything from a single letter to entire sentences... even html tags or C syntax... anything. The return value in ptr is a pointer to the phrase in memory...

Take a look in the help file for POIDE... Help -> Contents -> Standard #include files .... there are hundreds of functions you can use. If you aren't concerned about portability you can climb into the Private #includes as well which will more than double what's available to you.  If you expand that again into the windows API you will find something like another 30,000 function calls you can use... 

One lesson I learned very early on is that it's next to impossible to improve on these standard functions.  They've been developed and tested for years, by some of the best programmers in the business... it's pretty unlikely we'll best them.

Better you should know your resources than reinvent the wheel.
« Last Edit: June 09, 2012, 07:44:08 AM by CommonTater »

datatrap

  • Guest
Re: parsing a file
« Reply #5 on: June 09, 2012, 09:46:42 AM »
CommonTater you are right about not reinventing the wheel. I am terrible at looking up functions and learning them. I look through them and tell myself that will be useful later, then I cant remember what it was when I need to use it. lol

CommonTater

  • Guest
Re: parsing a file
« Reply #6 on: June 09, 2012, 02:46:49 PM »
CommonTater you are right about not reinventing the wheel. I am terrible at looking up functions and learning them. I look through them and tell myself that will be useful later, then I cant remember what it was when I need to use it. lol

 ;)  Trust me, you're not alone... I do pretty much the same thing.  It makes for slower coding but I've taken a policy of looking for helpful functions before writing each section of code. 
 
You know going in what you need to do, so look and see if it's already done for you... Eventually the most frequently used stuff stays with you.  Even the best of us has to realize there's so much in the CRT and API that nobody can hope to remember more than a small fraction of it.  Fortunately this stuff tends to be organized in to groups --string.h, stdio.h and so on-- so once we get into a general category we can narrow down our search very rapidly.
 
It's too bad it doesn't work like it does in the movies where some kid can sit down at a totally unfamiliar system, plug in his personal keyboard and make the thing stand up and dance the Irish jig all in less than 30 seconds*... In the real world programming is a lot of research and planning; our primary skills are problem analysis and "looking stuff up"... The mad-assed keyboarding part doesn't happen until way late in the process. I probably spend more time thinking about coding than I do writing actual code...
 
I used to have an employer who would get downright upset at me over the way I program. He would come past my workstation and I'd be sitting there staring off into space... "Hey wake the @%$# up and get to work"... "Gee thanks Ron, now I gotta start from scratch on that last function."  It never once crossed his mind that sometimes people need to think about things...  ::)
 
 
(* Movie reference:  Die Hard 4.0 )
 

datatrap

  • Guest
Re: parsing a file
« Reply #7 on: June 09, 2012, 08:17:51 PM »
That's funny. Are you an INTP?

CommonTater

  • Guest
Re: parsing a file
« Reply #8 on: June 09, 2012, 10:12:40 PM »
"INTP"

Wow... had to go looking to find what that one was... I suppose I am :D
 
http://en.wikipedia.org/wiki/INTP

« Last Edit: June 09, 2012, 10:24:17 PM by CommonTater »