Thread: Ignoring Lines and Continuing through Spaces,

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Ignoring Lines and Continuing through Spaces,

    Hey,

    I have a program that reads the files in its directory, and then puts them in a .txt file:

    Code:
         strncat(DirSpec, "\\*", 3);
         hFind = FindFirstFile(DirSpec, &FindFileData);
    
             int fileOutputCount = 0;
             ofstream a_file ( "oldnames.txt" );
             if (a_file.is_open() ) 
                   {
             	   while(FindNextFile(hFind, &FindFileData) != 0)
                   {
                          ++fileOutputCount;
                          if (fileOutputCount > 1 ) 
                             {
    
                                   a_file<<FindFileData.cFileName<<"\n";
                             }
                      }
            	   }
    
    FindClose(hFind);
    a_file.close();
    The problem is, I don't want it to count itself or two other file names. I've already taken care of . and .. with the fileOutputCount.

    Next problem, same program:

    I'm also reading line from another file, and they have spaces. I needed to read filenames, seperated by a new line. Currently, I have this:

    Code:
      fscanf(fp_fileNames, "%s", newFileName);
    I figure it has something to do with the "%s", but unfortunately, that was taken from another program, and searching for "%s" doesn't show anything useful.

    Thanks in advance,
    Blurr

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    "[^\n]"

    if I remember correct will read the string till the \n char
    or - use fgets - plain and simple
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    fgets doesn't work. It appends the /n char and I get this:

    Code:
    Error, file1.txt
      does not exist.
    And using "[^/n]" just makes the program loop an endless sea of errors.

    But thanks for the repy.
    Last edited by Blurr; 09-16-2007 at 07:33 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well, how to remove the \n when using fgets() is in the FAQ.

    Plus "sea of errors" really doesn't help us to figure out what you did wrong.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Thanks a lot! It works just how I wanted now!

Popular pages Recent additions subscribe to a feed