Thread: opening multiple files sequentially

  1. #1
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    opening multiple files sequentially

    i have pulled out a whole website and it has over 100 files
    (HTML).

    the problem is that the hyperlinks of that file are to the internet
    (having http:// .. .. /something.htm) I want to remove all that
    and put just the file (for eg. something.htm)

    All the HTML files have the same path to some extent, but
    just the filename changes ... so i know exactly what to remove
    (http:// .. ..... thing)


    instead of painfully doing "Replace" for each
    file in notepad and getting bald , i want to write a simple
    C program that can do this..

    I know how to open files

    fp = fopen("filename.htm", "a+")

    but here, i have to specify the filename... and i have to do this
    for 100s of files...

    so how do i make a for loop or something that opens all the files
    in a specified directory (say, the same directory as the EXE)
    and edit them one by one ...

    i hope i made my question clear ..
    Last edited by moonwalker; 08-20-2002 at 04:51 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are numerous ways to do this. You can even do this with simple redirection. Redirect the contents of a "ls" or a "dir" to a text file, parse it line by line for your file name. Then open that file, perform your task, close it, repeat.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    ok.. for example, i extracted the DIR of my c file directory..
    the TXT file looks like this..

    Code:
     Volume in drive C has no label.
     Volume Serial Number is AC7A-4705
    
     Directory of C:\MAHURSHI
    
    08/20/2002  12:29p      <DIR>          .
    08/20/2002  12:29p      <DIR>          ..
    06/14/2002  09:56p                 275 2AND3.CPP
    07/12/2002  10:06p                 176 ALPHAMIX.C
    08/03/2002  09:06p                 305 BLOCKSCOPE.C
    05/25/2002  06:54p                 269 BUBBLESORT.C
    05/27/2002  01:23p                 703 CALENDAR.C
    07/10/2002  08:20p                 746 CALENDAR.CPP
    08/08/2002  08:12p                 310 CAPITALIZE.CPP
    07/28/2002  08:24p                 773 CHECKNUM.CPP
    07/05/2002  07:06p                 575 CLASS.CPP
    08/08/2002  08:26p                 185 CMDLINEARG.C
    08/09/2002  11:52a                 544 COINCOUNT.CPP
    07/11/2002  08:58p                 339 CONNECTN.CPP
    06/30/2002  12:24a               1,844 DAY4DATE.CPP
    08/06/2002  07:07p                 124 DEFINEFUNCT.CPP
    08/04/2002  07:49a                 428 DUMP.CPP
    06/17/2002  07:07p                 971 EPOWERX.CPP
    08/08/2002  08:24p               1,580 FASTROOTS.CPP
    08/20/2002  12:29p                 334 FILEREADNOSCROLL.CPP
    06/20/2002  03:16p               1,221 FILES.CPP
    08/11/2002  08:34p                 306 FILEWRITE.CPP
    08/13/2002  11:14a                 317 FILEWRITEFGETS.CPP
    06/19/2002  07:17a                 223 FLOATMOD.CPP
    08/14/2002  10:26a                 199 FORSPECIAL.CPP
    05/23/2002  02:11p                 239 FUNC.CPP
    06/15/2002  09:54a               1,795 FUNCTIONS.CPP
    05/23/2002  03:06p                 195 GETC.CPP
    08/10/2002  10:32p                 171 GETKEYCODES.CPP
    06/21/2002  11:17p                 521 INTNPAD.CPP
    07/16/2002  09:46p               2,067 MAGICSQUARE.CPP
    08/07/2002  04:50p               7,445 MATCALC.CPP
    08/06/2002  07:10p               4,180 MATHGAME.CPP
    05/29/2002  12:40p               1,469 MATMULT.C
    08/03/2002  12:28p                 814 MATRIXSH.CPP
    08/18/2002  12:00p               2,172 MORSECODE.CPP
    05/29/2002  12:40p               1,543 MYMAT.C
    08/16/2002  08:10p                  53 MYPATH.BAT
    05/26/2002  11:44a                 294 NAME.C
    06/21/2002  01:34a                 465 NARENFACTORS.C
    08/04/2002  11:09a              43,220 NUMERO.CPP
    08/03/2002  12:24p      <DIR>          OTHER
    07/21/2002  05:07p               4,722 PONG.CPP
    07/26/2002  11:05p                 295 PRINTER.CPP
    05/25/2002  09:08p                 446 PROG1.C
    05/26/2002  11:34p                 502 PROG10.C
    05/26/2002  03:10p                 313 PROG11.C
    05/25/2002  09:26p                 324 PROG2.C
    05/25/2002  09:29p                 324 PROG3.C
    05/25/2002  09:56p                 432 PROG4.C
    05/26/2002  10:16a                 172 PROG6a.C
    05/26/2002  06:13p                 208 PROG6b.C
    05/26/2002  03:59p                 168 PROG6c.C
    05/26/2002  06:17p                 181 PROG6d.C
    05/26/2002  12:51p                 191 PROG6e.C
    05/26/2002  03:56p               1,028 PROG7.C
    05/26/2002  06:57p                 431 PROG8a.C
    05/26/2002  06:58p                 432 PROG8b.C
    05/26/2002  10:14p                 489 PROG9a.C
    05/26/2002  10:15p                 490 PROG9b.C
    08/08/2002  08:19a               4,116 QUICKMAT.CPP
    06/27/2002  09:46p                 856 RANDOMGAME.CPP
    06/29/2002  11:17a                 754 RECURSIVE.CPP
    06/18/2002  05:01p               1,361 ROUNDFLOAT.CPP
    07/07/2002  12:10a                 270 RUN.CPP
    07/29/2002  07:35p               6,532 SPACECOMBAT.CPP
    07/07/2002  08:19a               3,538 SPOUSE.CPP
    07/02/2002  11:46a               1,333 SQCU1000.CPP
    08/15/2002  03:51p                 533 STARDROP.BAK
    08/15/2002  03:52p                 513 STARDROP.CPP
    08/09/2002  11:04p                 257 STRUCT.CPP
    06/21/2002  11:58p               1,158 STRUCTURES.CPP
    07/25/2002  10:42p               5,133 TEST.CPP
    07/26/2002  08:47a               5,133 TEXTPONG.CPP
    08/20/2002  04:30p      <DIR>          TRASH
    08/08/2002  08:23p                 722 VARIABLEARG.C
    06/15/2002  10:52p                 686 VOWEL.CPP
    05/23/2002  09:02p                 139 WHILE.CPP
    08/04/2002  10:47a               1,486 WORDWRAP.CPP
    07/10/2002  08:22p               5,603 YEARCALENDAR.CPP
                  76 File(s)        130,661 bytes
                   4 Dir(s)   3,864,576,512 bytes free
    now how do i pull out the file names of *.c and *.cpp files
    out of this text file ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take about four seconds and look at one of the lines. How do you tell if it has a .c or .cpp extension? Ah hah! You look at the trailing .*

    As such, the algorythm should be fairly simple:

    1) read a line
    2) go backwards until you find a '.' --OR-- check 2 characters from the end to see if it is a '.', if not, check 4 characters back.
    3) Once you've found the extension, read the next 1 or 3 characters to see if it is 'cpp' or 'c'.
    Once it is, you know that line is good. As such, back up until you hit some white space. When you reach whitespace, you know you've found the beginning of the word.
    4) copy the word into a buffer

    strrchr should work fine to get you to the extension.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    PHP Code:
    DIR *directory;
    struct dirent *direntry;

    directory opendir (".");

    if (
    directory)
    {
      while ((
    direntry readdir (directory)))
        if (
    strstr (direntry->d_name".wav"))
          
    do_stuff (direntry->d_name);
      
    closedir (directory);
    }

    else
      
    printf ("error opening current directory\n"); 
    hello, internet!

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: hmm

    Originally posted by moonwalker
    ok.. for example, i extracted the DIR of my c file directory..
    the TXT file looks like this..


    08/15/2002 03:52p 513 STARDROP.CPP
    08/09/2002 11:04p 257 STRUCT.CPP
    06/21/2002 11:58p 1,158 STRUCTURES.CPP
    ...

    now how do i pull out the file names of *.c and *.cpp files
    out of this text file ?
    dir /b *.c *.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Notepad++ opening ActionScript files
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-26-2008, 03:49 PM
  2. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  3. opening files sequentially
    By barneygumble742 in forum C++ Programming
    Replies: 9
    Last Post: 12-21-2006, 11:26 AM
  4. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM