finding and copying a txt file and making a new file with the copy

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1840 Views - Last Post: 12 October 2012 - 08:35 PM Rate Topic: -----

#1 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

finding and copying a txt file and making a new file with the copy

Posted 06 October 2012 - 07:11 PM

ok here it is, ive been compiling a program that finds text files, it finds all of the files with "*.txt", but i want to copy the contents of a specific txt filename(s) and put them into another txt file, my computer has 100000000's of folders all with files of the same name so if i search mostly junk comes up, i figured if i do this a few times to each of the main filenames i can read it and actually be able to use the information.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <queue>
#include        <stdio.h>
#include		<stdlib.h>
#include        <dir.h>
using namespace std;
void process(std::string const &path, WIN32_FIND_DATA const &file) {
   std::cout << path << file.cFileName << "\n";//i think this looks cool
    char*transfer;//for the txt of the to be saved 
string name;
	string searchPattern = "*LitNotes.txt";// one of the filenames i need to organize
	string fullSearchPath = path + searchPattern;
	WIN32_FIND_DATA FindData;
	HANDLE hFind;

	hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );

	if( hFind == INVALID_HANDLE_VALUE )
	{
cout<<"Still searching for LitNotes. \n";
}

	do
	{
		string filePath = path + FindData.cFileName;
		ifstream in( filePath.c_str() );
		if( in )
		{ifstream myfile;
	myfile.open (filePath.c_str());
		
		 CopyFile(transfer,(filePath.c_str()), FALSE);//,makes it false? right//FALSE?
	myfile.close();
if(transfer == FALSE){
           ofstream OuT;
           OuT.open("AllLit.txt");
           OuT<<transfer;
           OuT.close();
           cout << "successfully transferred"  << FindData.cFileName << "\n";
		}
			}
	while( FindNextFile(hFind, &FindData) > 0 );

	if( GetLastError() != ERROR_NO_MORE_FILES )
	{
		cout << "done searching here!!\n" ;
	}
}
/////////////////////////////////
void find_file(std::string const &folder_name, std::string const &fmask) {
    HANDLE finder;          // for FindFirstFile
    WIN32_FIND_DATA file;   // data about current file.
    std::priority_queue<std::string, std::vector<std::string>,
                       std::greater<std::string> > dirs;
    dirs.push(folder_name); // start with passed directory

    do {
        std::string path = dirs.top();// retrieve directory to search
        dirs.pop();

        if (path[path.size()-1] != '\\')  // normalize the name.
            path += "\\";

        std::string mask = path + fmask;    // create mask for searching

        // traverse a directory. Search for sub-dirs separately, because we
        // don't want a mask to apply to directory names. "*.txt" should find
        // "a\b.cpp", even though "a" doesn't match "*.txt".
        //
        // First search for files:
        if (HNULL==(finder=FindFirstFile(mask.c_str(), &file)))
            continue;

        do {
            if (!(file.dwFileAttributes & A_DIR))
                process(path, file);
        } while (FindNextFile(finder, &file));

        FindClose(finder);

        // Then search for subdirectories:
        if (HNULL==(finder=FindFirstFile((path + "*").c_str(), &file)))
            continue;
        do {
            if ((file.dwFileAttributes & A_DIR) && (file.cFileName[0] != '.'))
                dirs.push(path + file.cFileName);
        } while (FindNextFile(finder, &file));
        FindClose(finder);
    } while (!dirs.empty());
}
int main(int argc, char **argv) {
    if (argc > 2)
        find_file(argv[1], argv[2]);
    else
        find_file("C:\\", "*");

    return 0;
}


the end keeps it going back for subdirectories, the moral of the story is not to copy and paste and recopy and paste into the same folder with the same files(names) in them and then just try to follow whats going on, i was sharing notes with some friends and it got confusing but im not reading 200 copies of semester notes that are all basically the same, i need the little bits for my final in like 2 weeks.

Is This A Good Question/Topic? 0
  • +

Replies To: finding and copying a txt file and making a new file with the copy

#2 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 06 October 2012 - 07:56 PM

#include <windows.h>
const HANDLE HNULL = INVALID_HANDLE_VALUE;
const int A_DIR = FILE_ATTRIBUTE_DIRECTORY;
i forgot thoose three lines, basically the same errors come up.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: finding and copying a txt file and making a new file with the copy

Posted 06 October 2012 - 09:23 PM

You never really stated what problem you are encountering, and what you have tried so far to resolve them.
Was This Post Helpful? 0
  • +
  • -

#4 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 07 October 2012 - 08:08 PM

ifstream myfile;
033
    myfile.open (filePath.c_str());
034
         
035
         CopyFile(transfer,(filePath.c_str()), FALSE);//,makes it false? right//FALSE?
036
    myfile.close();
037
if(transfer == FALSE){
038
           ofstream OuT;
039
           OuT.open("AllLit.txt");
040
           OuT<<transfer;
041
           OuT.close();


right there is the problem, whenever it gets to a file with a name specified it crashes instead of copying the file to the new file... this is hell i wish i was more organized.
Was This Post Helpful? 0
  • +
  • -

#5 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 07 October 2012 - 08:28 PM

i have tried putting argc and argv into the () after void process, ive tried changing the thing from string to char and back, char has less errors, and ive tried using ifstream and ofstream together, im honestly just stumped, if you watch the program run it crashes as soon as you get to a file with the specified name. good thing i still have the books, but ima tell you poets are bad when it comes to me being able to filter through their quotes(i know, they arent bad, im just not good) Any information just pointing me in the right direction will be UBER appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,218
  • Joined: 20-March 10

Re: finding and copying a txt file and making a new file with the copy

Posted 07 October 2012 - 09:38 PM

In the CopyFile function setting bfailifexists to FALSE

does not make transfer false...

Also you are not allocating any memory for transfer causing a buffer overrun later on ...

You are not even intialising transfer even..... are you ?

formatting your code will make it easier to spot missing brackets and the like
and will get you more responses..

Your formatted code

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <queue>
#include        <stdio.h>
#include		<stdlib.h>
#include        <dir.h>
#include <windows.h>
const HANDLE HNULL = INVALID_HANDLE_VALUE;
const int A_DIR = FILE_ATTRIBUTE_DIRECTORY;
using namespace std;
void process(std::string const &path, WIN32_FIND_DATA const &file)
{
    std::cout << path << file.cFileName << "\n";//i think this looks cool
    char*transfer;//for the txt of the to be saved
    string name;
    string searchPattern = "*LitNotes.txt";// one of the filenames i need to organize
    string fullSearchPath = path + searchPattern;
    WIN32_FIND_DATA FindData;
    HANDLE hFind;

    hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );

    if( hFind == INVALID_HANDLE_VALUE )
    {
        cout<<"Still searching for LitNotes. \n";
    }

    do
    {
        string filePath = path + FindData.cFileName;
        ifstream in( filePath.c_str() );
        if( in )
        {
            ifstream myfile;
            myfile.open (filePath.c_str());

            CopyFile((filePath.c_str()),transfer, FALSE);//,makes it false? right//FALSE?
            myfile.close();
            if(transfer == FALSE)
            {
                ofstream OuT;
                OuT.open("AllLit.txt");
                OuT<<transfer;
                OuT.close();
                cout << "successfully transferred"  << FindData.cFileName << "\n";
            }
        }
    }
        while( FindNextFile(hFind, &FindData) > 0 );

        if( GetLastError() != ERROR_NO_MORE_FILES )
        {
            cout << "done searching here!!\n" ;
        }
    }
/////////////////////////////////
    void find_file(std::string const &folder_name, std::string const &fmask)
    {
        HANDLE finder;          // for FindFirstFile
        WIN32_FIND_DATA file;   // data about current file.
        std::priority_queue<std::string, std::vector<std::string>,
        std::greater<std::string> > dirs;
        dirs.push(folder_name); // start with passed directory

        do
        {
            std::string path = dirs.top();// retrieve directory to search
            dirs.pop();

            if (path[path.size()-1] != '\\')  // normalize the name.
                path += "\\";

            std::string mask = path + fmask;    // create mask for searching

            // traverse a directory. Search for sub-dirs separately, because we
            // don't want a mask to apply to directory names. "*.txt" should find
            // "a\b.cpp", even though "a" doesn't match "*.txt".
            //
            // First search for files:
            if (HNULL==(finder=FindFirstFile(mask.c_str(), &file)))
                continue;

            do
            {
                if (!(file.dwFileAttributes & A_DIR))
                    process(path, file);
            }
            while (FindNextFile(finder, &file));

            FindClose(finder);

            // Then search for subdirectories:
            if (HNULL==(finder=FindFirstFile((path + "*").c_str(), &file)))
                continue;
            do
            {
                if ((file.dwFileAttributes & A_DIR) && (file.cFileName[0] != '.'))
                    dirs.push(path + file.cFileName);
            }
            while (FindNextFile(finder, &file));
            FindClose(finder);
        }
        while (!dirs.empty());
    }


    int main(int argc, char **argv)
    {
        if (argc > 2)
            find_file(argv[1], argv[2]);
        else
            find_file("C:\\", "*");

        return 0;
    }





Snoopy.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: finding and copying a txt file and making a new file with the copy

Posted 07 October 2012 - 10:50 PM

CopyFile()'s second parameter is supposed to be a buffer containing the filename of the destination file. http://msdn.microsof...1(v=vs.85).aspx

You are using the Win32 API as if the second parameter were a buffer where the contents of the file would be read into so that you could later write it out into "AllLit.txt".

Even worse, your logic is doubly flawed. Let's say that the transfer buffer did contain the contents of the file. As snoopy11 pointed out you didn't allocate space for the buffer, and additionally, if you did successfully allocate space for the buffer, the pointer would not be null. Since both FALSE and NULL would evaluate to a zero value, the if condition where you check to see if transfer is FALSE, would then mean that you are trying to read a null pointer to write out into "AllLit.txt".
Was This Post Helpful? 1
  • +
  • -

#8 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 12:00 AM

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <queue>
#include        <stdio.h>
#include		<stdlib.h>
#include        <dir.h>
#include <windows.h>
const HANDLE HNULL = INVALID_HANDLE_VALUE;
const int A_DIR = FILE_ATTRIBUTE_DIRECTORY;
using namespace std;
void process(std::string const &path, WIN32_FIND_DATA const &file)//,int argc, char *argv[]
{
    std::cout << path << file.cFileName << "\n";//i think this looks cool
    char*transfer;//for the txt of the to be saved
    string name;
    string searchPattern = "*alllit.process.exe";// one of the filenames i need to organize
    string fullSearchPath = path + searchPattern;
    WIN32_FIND_DATA FindData;
    HANDLE hFind;

    hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );

    if( hFind == INVALID_HANDLE_VALUE )
    {
        cout<<"Still searching for LitNotes. \n";
    }

    do
    {
        string filePath = path + FindData.cFileName;
        ifstream in( filePath.c_str() );
        if( in )
        {
            ifstream myfile;
            myfile.open (filePath.c_str());

            CopyFile((filePath.c_str()),"AllLITTs.exe", TRUE);//,makes it false? right//FALSE?
            myfile.close();
          /*  if(transfer == FALSE)
            {
                ofstream OuT;
                OuT.open("AllLit.exe");
                OuT<<transfer;
                OuT.close();
                cout << "successfully transferred"  << FindData.cFileName << "\n";
            }*/
        }
    }
        while( FindNextFile(hFind, &FindData) > 0 );

        if( GetLastError() != ERROR_NO_MORE_FILES )
        {
            cout << "done searching here!!\n" ;
        }
    }

this sort of worked just now, lemme test it a bit more
Was This Post Helpful? 0
  • +
  • -

#9 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 01:05 AM

yea idk how to make it compute the txt of more than one alllit.txt, it just uses the first one used, i was hoping it would just negate the overlap, but i have not the time or energy to code that if it didnt on its own. poets are sort of easier to search on the internet but if you could tell me to keep the program from trunicating the txt after the first find
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,218
  • Joined: 20-March 10

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 05:12 AM

Hi,

I dont know what you are trying to achieve.
It could be one of two things..

1. you're trying to get several litnotes.txt into one Allits.txt file by appending them into one file.

2. you're trying to copy several files into several Allits.txt file ie
Allits(1).txt
Allits(2).txt
Allits(3).txt
etc...

which is it 1 or 2 ?

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 04:51 PM

I want to append all of the text files to one alllit.txt//thats why it is alllit.
thats the one good part to my Litnotes.tct, they all have the same ending.
Was This Post Helpful? 0
  • +
  • -

#12 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 05:01 PM

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <queue>
#include        <stdio.h>
#include        <stdlib.h>
#include        <dir.h>
#include <windows.h>
const HANDLE HNULL = INVALID_HANDLE_VALUE;
const int A_DIR = FILE_ATTRIBUTE_DIRECTORY;
using namespace std;
void process(std::string const &path, WIN32_FIND_DATA const &file)//,int argc, char *argv[]
	{
	    std::cout << path << file.cFileName << "\n";//i think this looks cool
	    char*transfer;//for the txt of the to be saved
	    string name;
	    string searchPattern = "*alllit.txt";// one of the filenames i need to organize
   string fullSearchPath = path + searchPattern;
	    WIN32_FIND_DATA FindData;
	    HANDLE hFind;
	 
	    hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );
	 
	    if( hFind == INVALID_HANDLE_VALUE )
	    {
	        cout<<"Still searching for LitNotes. \n";
	    }
	 
	    do
	    {
	        string filePath = path + FindData.cFileName;
	        ifstream in( filePath.c_str() );
	        if( in )
	        {
	            ifstream myfile;
	            myfile.open (filePath.c_str());
	 
	            CopyFile((filePath.c_str()),"AllLITTs.txt", TRUE);//,makes it false? right//FALSE?
	            myfile.close();

	        }
	    }
	        while( FindNextFile(hFind, &FindData) > 0 );
	 
	        if( GetLastError() != ERROR_NO_MORE_FILES )
	        {
	            cout << "done searching here!!\n" ;
	        }
	    }
	          /*  if(transfer == FALSE)
43	            {
44	                ofstream OuT;
45	                OuT.open("AllLit.txt");
46	                OuT<<transfer;
47	                OuT.close();
48	                cout << "successfully transferred"  << FindData.cFileName << "\n";
49	            }*/
void find_file(std::string const &folder_name, std::string const &fmask) {
    HANDLE finder;          // for FindFirstFile
	    WIN32_FIND_DATA file;   // data about current file.
    std::priority_queue<std::string, std::vector<std::string>,
                       std::greater<std::string> > dirs;
	    dirs.push(folder_name); // start with passed directory
	 
    do {
	        std::string path = dirs.top();// retrieve directory to search
        dirs.pop();
 
        if (path[path.size()-1] != '\\')  // normalize the name.
            path += "\\";
 
        std::string mask = path + fmask;    // create mask for searching
 
        // traverse a directory. Search for sub-dirs separately, because we
	        // don't want a mask to apply to directory names. "*.txt" should find
        // "a\b.txt", even though "a" doesn't match "*.txt".
       //
	        // First search for files:
        if (HNULL==(finder=FindFirstFile(mask.c_str(), &file)))
           continue;
 
        do {
	            if (!(file.dwFileAttributes & A_DIR))
	                process(path, file);
	        } while (FindNextFile(finder, &file));
	 
        FindClose(finder);
 
        // Then search for subdirectories:
        if (HNULL==(finder=FindFirstFile((path + "*").c_str(), &file)))
	            continue;
	        do {
	            if ((file.dwFileAttributes & A_DIR) && (file.cFileName[0] != '.'))
	                dirs.push(path + file.cFileName);
	        } while (FindNextFile(finder, &file));
	        FindClose(finder);
	    } while (!dirs.empty());
	}
	int main(int argc, char **argv) {
	    if (argc > 2)
	        find_file(argv[1], argv[2]);
	    else
	        find_file("C:\\", "*");
	 
	    return 0;
	}
yes i want to do (1)
if you can give me a good link to read or an example i would greatly appreciate it.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 05:02 PM

Then the Win32 CopyFile() is not going to help you. You'll have to read the contents of your input file and append them to the end of your output file. I suggest reading them 4K or 8K bytes at a time since you aren't trying to parse the contents anyway.
Was This Post Helpful? 1
  • +
  • -

#14 psilocybin420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-September 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 07:11 PM

ive only been coding for about a month now, can you point me to some information on appending data
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: finding and copying a txt file and making a new file with the copy

Posted 09 October 2012 - 11:32 PM

Somehow, in your original post, you were using the ofstream class. Didn't you look at the documentation for that class? It has an append mode if you pass the correct parameters to the construct or to open().
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2