Ignoring Lines and Continuing through Spaces,

This is a discussion on Ignoring Lines and Continuing through Spaces, within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a program that reads the files in its directory, and then puts them in a .txt file: ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    "[^\n]"

    if I remember correct will read the string till the \n char
    or - use fgets - plain and simple
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

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