Results 1 to 4 of 4

Thread: writing files to txtfile

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    writing files to txtfile

    Code:
    #define MAX_PATH 256;
    
    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <fstream.h>
    
    
    void dir(char *path);
    
    int main()
    {
    dir("\\");
    	return 0;
    }
    
    void dir(char *path)
    {
    	FILE *fp;
    	char dirname[MAX_PATH];
    	HANDLE fhwnd;
    	WIN32_FIND_DATA WFD;
    
    
    	SetCurrentDirectory(path);
    
    	fhwnd = FindFirstFile("*.*", &WFD);
    
    	fp = fopen("C:/Matt/files2.doc","w");
    
    	while(FindNextFile(fhwnd,&WFD))
    	{
    		if(
    			(WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (strcmp(WFD.cFileName, "..") && strcmp(WFD.cFileName, "."))
    			)
    		{
    			GetCurrentDirectory(MAX_PATH,dirname);
    			if(strncmp(&dirname[strlen(dirname) - 1], "\\", 1)){
    				strcat(dirname, "\\");
    			}
    
    			strcat(dirname, WFD.cFileName);
    			cout << dirname;
    			fprintf(fp,dirname);
    			fprintf(fp, "\n");
    			cout << endl;
    			dir(dirname);
    		}
    
    	}
    
    	FindClose(fhwnd);
    	fclose(fp);
    }
    I cannot see all the results when this finds all the files in the console window. So I made it write it to a file but it only writes a couple on the file path's. Why is it only printing a few?? Thanks
    Matt

  2. #2
    Member
    Join Date
    Feb 2001
    Posts
    57

    Here is the problem

    This line is goofing the code up:
    Code:
    dir(dirname);
    This line calls the dir function again (calls itself) and so it reopens the file that you are writing to again.
    Code:
    fp = fopen("C:/Matt/files2.doc","w");
    Does that help?


    chilibean

  3. #3

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    The if statement checks to see weather its a directory. If so then call the function with that directory.
    Matt

  4. #4

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    problem solved thanks!
    Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width