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