Hello and HELP!!!!

I'm in CSC250. I'm writing a prog that needs to do the following:

1- read files from the directory, avoiding sub-directories (along w/ _chdir)
2- read the contents of the file (as binary)
3- extract info from the files
4- modify the file name to reflect info found

I have to find image files that have had their file extensions removed, determine what type of image it is and the dimensions, then append this info to the file name.

My functions that determine the image type are good to go.

My problem is that I can't get the blasted file(s) read in! I can read a single file (e.g."junk.txt") and do the comparisons, but I can't get the directory file thing to work. Also, one type of file (jpg) will be of an indeterminate size.

And lastly, this caveat was posted by my prof:

ATTENTION: all reads from the binary file must be into a character type, not integer types

Example: char sb1; unsigned char ub2;

file.read( (char *) &sb1, sizeof(char) );

file.read( (char *) &ub2, sizeof(unsigned char));

...........

I sit in class, pay close attention, and take notes. Sadly, I'm at a loss. completely. (almost)

Here's the code I have thus far that pertains to the directory reading part:

#include <iostream>
#include <fstream>
#include <io.h>
#include <direct.h>
#include <string>
#include <cstring>

using namespace std;
int main ( int argc, char * argv[] )
{



    ifstream file ;
    ofstream fout ;
    int i ;
    int fileType;
    //char fileName[20] = 0;

    if ( argc != 4 ) //test that ***4*** arguments are present
    {
        cout << "Program usage:  prog1.exe, dir1, dir2, dir3";
        cout << endl;
        cout << "Exiting now." << endl << endl;
        // return -1;
    }

    if  ( argc < 2 )
    {
        cout << "No directories passed. Program Exiting!" << endl;
        exit ( 0 );
    }


    for ( i = 0; i < argc; i++ ) //list all the arguments, including
        cout << argv[i] << endl;  //program name

    for ( i = 1; i <= argc; i++ )
    {
        int dirNum = 0;

        file.open ( argv[i] );   //open first argument–inputfile (dir)

        if ( !file )
        {
            if ( i <= argc )
            {
                cout << "Unable to open directory" << i << endl;
                cout << "Changing to directory" << i + 1 << endl;
                int _chdir ( const char * dirname );
            }
        }
    }


    if( ( c_file.attrb && 16 )  != 1 ) // while the file is not a dir or sub dir...
    {

        //         read in file name to char fileName
        _finddata_t c_file;
        intptr_t hFile;

        _chdir ( “c: \\dir1” );

        hFile = _findfirst ( "*.*", &c_file );
        cout << c_file.name << endl;
        while ( _findnext ( hFile, &c_file ) == 0 )
        {
            cout << c_file.name << endl;
        }
    }

    return (0);
}

Thank you in advance for your help. This is already late because I'm stubborn & thought I could figure it out myself.

Bless you,
Christina

My problem is that I can't get the blasted file(s) read in! I can read a single file (e.g."junk.txt") and do the comparisons, but I can't get the directory file thing to work. Also, one type of file (jpg) will be of an indeterminate size.

What does "directory file thing" mean exactly?
What part of the program fails? What is the last line of output you see?

>>1- read files from the directory, avoiding sub-directories (along w/ _chdir)

That means to use directory functions to get the names of all the files in a directory. How to do that will depend on what operating system you are using.

  1. MS-Windows: FindFirstFile() and FindNextFile()
  2. *nix: Opendir() and ReadDir()

Google for those functions and you will find out how to use them. I have posted example programs for both operating systems here in Code Snippets, but they are probabgly more advanced than what you can handle at the moment.

AD,
The OP is using the direct.h header so she is probably using the Digital Mars libraries. There can be various reasons the code is failing, incorrect input, spaces between directory names, etc. So without information from the OP on where it is actually failing, we don't have any way of resolving this issue.

I'm sorry I wasn't clear. I'm not sure what you mean by the "OP". Here are the errors when I try to compile. Thx so much! CS

1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(57) : error C2065: 'c_file' : undeclared identifier
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(57) : error C2228: left of '.attrb' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2065: '“c' : undeclared identifier
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2143: syntax error : missing ')' before ':'
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2017: illegal escape sequence
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2017: illegal escape sequence
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2059: syntax error : ')'
1>Build log was saved at "file://c:\Users\1014034\Documents\Visual Studio 2008\Projects\p1 2\Debug\BuildLog.htm"
1>p1 2 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

>>I'm sorry I wasn't clear. I'm not sure what you mean by the "OP".
OP = Original Poster, in otherwids, you.

>>error C2065: 'c_file' : undeclared identifier
That means you failed to declare a variable called c_file, so the compiler has no idea what you are talking about on that line.

line 64: remove the space after c:

OP means Original Poster, and that is you.

1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(57) : error C2065: 'c_file' : undeclared identifier
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(57) : error C2228: left of '.attrb' must have class/struct/union
1> type is ''unknown-type''

c_file is being used before declaring it. That is the reason for the above error.

1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2065: '“c' : undeclared identifier
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2143: syntax error : missing ')' before ':'
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2017: illegal escape sequence
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2017: illegal escape sequence
1>c:\users\1014034\documents\visual studio 2008\projects\p1 2\p1.cpp(64) : error C2059: syntax error : ')'

The “” in _chdir ( “c: \\dir1” ); are in an un-ascii code. That is why the errors in line 64 shows up. The compiler can't process it.
Replace it to _chdir ( "c:\\dir1" ); and errors in line 64 should dissappear. Better to type it by hand and see.

Anyway the below snippet should work.

_finddata_t c_file;
    intptr_t hFile;
    

    _chdir ( "c:\\dir1" );
    // Find any file in current directory 
    if( (hFile = _findfirst( "*.*", &c_file )) == -1 ){
        cout <<"Error! :" << errno  << endl; // This doesn't mean current directory is empty.  
    }
    else
    {
        do {
            // if the file is not a sub dir...
            if( ( c_file.attrib & _A_SUBDIR ) != _A_SUBDIR ){ // _A_SUBDIR = 0x10 (16 in decimal). Also note the single &. 
                cout << c_file.name << endl;
            }
        } while( _findnext( hFile, &c_file ) == 0 ); // find next file
        _findclose( hFile );
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.