Thread: Outputing 2 times?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    Outputing 2 times?

    Ok people, i made this code (it is not complete yet) to hold all files and subdirectories inside a master directory

    But, for some reason the output information come 2 times (compile it and you will se).
    Why? Any information you guys need, just ask me (sorry if my code is too much confuse, im new to c++).


    Code:
    #define _WIN32_WINNT 0x0400
    
    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //class to hold info of each file/folder
    class database{
    public:
    string name;
    bool is_folder;
    long lenght;
    };
    
    
    //this int is used to not overwrite other class elements when messing with subdirectories
    int last_i;
    int b = 0;
    
    //function that put files in the database lass and cout some info
    void search_files(database& files, string test);
    
    //function that takes the file leght (not implemented yet)
    void contar_tamanho(database& files, const char* filename);
    
    #include "main.h"
    
    int main()
    {
    database files[99];
    int i = 0;
    
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind;
    	LPCSTR hFile;
    
    //creates one string for the user put the directory to be examinated and add a \* in the end.
    	string file;
    	cout << "Select a directory to be examinated:" << endl;
    	cin >> file;
    	file += "\\*";
    
    	hFind = FindFirstFile(file.c_str(), &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE) 
    {
    	printf ("Invalid File Handle. GetLastError reports %d\n", 
    	GetLastError ());
       	system("Pause");
      	return (0);
    } 
      else 
    {
    	cout << "File found: " << FindFileData.cFileName << "." << endl;
    	for (i = 0; FindNextFile(hFind, &FindFileData) != 0; i++) 
    	{
    string dottest = FindFileData.cFileName;
    	if (dottest != "..") search_files(files[i], FindFileData.cFileName);
    	else i--;
    }
    
    //here the files of each subdirectory is examinated.
    	for (int a = 0; a <= 99; a++){
    
    	if (files[a].is_folder == true)
    {
    	string subdirectory = file.substr(0, file.length()-1) + files[a].name + "\\*";
    	cout << endl << subdirectory.substr(0, subdirectory.length()-1) << endl;
    	hFind = FindFirstFile(subdirectory.c_str(), &FindFileData);
    	for (i = last_i+1; FindNextFile(hFind, &FindFileData) != 0; i++)
    				{
    	string dottest = FindFileData.cFileName;
    	if (dottest != "..") search_files(files[i], FindFileData.cFileName);
    	else i--;
    
    }
     }
      }
    
    //subdiretory check done... now closing the program
    	FindClose(hFind);
    	system("Pause");
    	return (1);
      }
    }
    
    void search_files(database& files, string test){
    	arquivos.name = test;
    	string::size_type dot = teste.find( ".", 0 );
    	if (dot != string::npos){ cout << "File found: " << test << endl; files.is_folder = false;}
    	else { cout << "Folder found: " << test << endl; files.is_folder = true;}
    	if (b == 0) last_i = 0;
    	else last_i++;
    	b++;
    }
    
    //this function is not being used yet
    void contar_tamanho(database& files, const char* filename)
    {
    	long size;
    	FILE *f;
    
    	f = fopen(filename, "rb");
    
    	fseek(f, 0, SEEK_END);
    	size = ftell(f);
    	fclose(f);
    	arquivos.lenght = size;
    }
    error image:

    http://img181.imageshack.us/img181/6295/imagemor2.jpg
    Last edited by Tropicalia; 09-23-2006 at 09:21 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you checking for both "." and ".." and skipping both?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    No, Im not skipping them yet, but is it the problem?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure. It is amazing how much comments and variable names help when reading code, and unfortunately I don't understand those variable names or comments.

    If you are recursing on the sub folders, and you recurse on the "." folder, then you will repeat the current folder again because "." is just the current folder.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    well, i'll try it in a sec.
    but i made the comments in portuguese and in english :P

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You did. I completely missed that.

    I do think the problem is that you need to always skip "." and "..". Even in your error message you have "c:\coco\\" for the second one, meaning it is probably processing "c:\coco\.".

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Ok, now Im filtering the ".." and the problem continues
    (actually the "." is not needed because it is taken by the FindFirstFile function)

    Code:
    string dottest = FindFileData.cFileName;
    	if (dottest != "..") examinar(arquivos[i], FindFileData.cFileName);
    	else i--;
    i also made that change in the first thread

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Ok, all the code is in english now.
    Hope it helps you guys to understand it

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Work on the indentation. Either use tabs (like I do), or use spaces (like others do). Don't mix them. Not everyone's viewers' tabs are set to 4, 8 or whatever number of spaces your editor uses. I won't really take much time to look at the code until it's properly formatted and pleasing to the eye.

    Oh, and it's spelled "length" not "leght"

    By the way, since you're already using Windows functions, you can use the API to query the filesize directly, without needing to open the file at all.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Edited, is it right now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  2. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  3. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  4. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM
  5. Character that appears max times in string
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-15-2004, 12:06 PM