C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 28th, 2008, 06:29 AM
MrSquiggles MrSquiggles is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 MrSquiggles User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 54 sec
Reputation Power: 0
C++ opening all .txt files in a folder one by one

I've honest to God put in more effort than a computer retarded person should ever have to put into this. I've even tried to diligently demonstrate that I need walking hand to others in 2 forums, now I just want a sympathy handout.

I've concluded I'm too just to computer stupid, like I know it is right in front of me.
Code:
//This is the GIANT history of my confusion 

Read links below, I really tried. Here is what I have and I know it is literally right on this concept. I just want to open ALL the files in the folder. This lets me open just one file in a folder and I can list the entire directory or just the filetype with "*.txt". But really I just want to open all the .txt files in the folder one by one.


#include <windows.h> 
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inputFile;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile("C:\\*.txt", &FindData);


while (FindNextFile(hFind, &FindData))
{     
inputFile.open ("th1.txt");        //I can only open single files up 
}                                  //how do I open all files in direc
    
  inputFile.close();
  system("pause");
  return 0;
}



I've honestly tried to understand this for many days and I just don't get it and when I went two boards in guidance of help on 2 webpages I get sort of answers because I think people really think I'm trying to have them do school work for me, I desperately want to just get this concept to work.

"   1. We are sorry, new user accounts are not permitted to create posts containing URLs. Please review our forum rules"

I paste what I've done so far look for help show you how much I really tried to understand a syntax issue, I absolutely understand the semantics of this open close open close...
_____________________
    FindData.cFileName=1.txt and works on cout, but it won't let me pass it into inputFile.open().


    Here is situation, I'm attempting to edit multiple files one by one and this below doesn't work. It just ignores the 3rd line (compiles fine and runs)

    string detects=FindData.cFileName;
    cout<<FindData.cFileName;
    inputFile.open (detects);


    I also tried
    inputFile.open(FindData.cFileName);
    (same issue)


    But this works
    cout<<"FindData.cFileName"
    inputFile.open ("1.txt");

    Any advice much appreciated
______________________________
e so far.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
int count;
const int Baseball = 40;
int BB[Baseball];
string playername[40];

ifstream inputFile;
inputFile.open ("2.txt");
for (count = 0; count < Baseball; count++) {
inputFile >> BB[count];
inputFile >> playername[count];
}
inputFile.close();
system("pause");
return 0;
__________________
I'm pulling multiple files to get the same kind of information into an array. There is a lot off Classroom file names and they change, and each has 40 students in them so I need to open each file and goto the next, for just a single file this would work.

inputFile.open ("Classroom1.txt");
for (count = 0; count < Forty; count++)
{
inputFile >> rank[Forty];
inputFile >> name[studentcount];
}
__________________
Here is another idea I tried.. Using a string and change the string name loop, but it won't let me and to pass that string into the (). Tried this and it failed too so I'm absolutely tried on this issue

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string bob="1.txt";

ifstream in(bob);
if(!in) {
cout << "Cannot open file.\n";
return 1;
}

char c;
while(in) {
in.get(c);
cout << c;
}


in.close();
return 0;
}
__________________________
I've gotten very close with that method in fact near replica of what it. And believe me when I say I'm not too lazy too care, just too stupid to understand. Heres what I have.

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{

HANDLE hFind;
WIN32_FIND_DATA FindData;

hFind = FindFirstFile("C:\\Users\\Satellite\\Des... &FindData);
cout << FindData.cFileName << endl;

while (FindNextFile(hFind, &FindData))
{
cout << FindData.cFileName << endl;
// FILE * hFile = fopen((char *) W32_FIND_DATA.cFileName, "rt");

}

//fclose(hFile;
system("pause");
return 0;
}
______________________

I'm just desperate at this point I've honestly tried 2 yahoo answers and one computing.net. I also have about 60 almost .cpp files demonstrating this I can even rar them all up to show how aimless I've been on this issue.

It just sounds so basic, open a text file, go to next text file, and continue on. and do a in>> to get all the stuff from those text files. I'm utterly compfused right now appreciate anything. Just please no msdn's they don't help me.

Last edited by MrSquiggles : May 28th, 2008 at 09:16 AM. Reason: trying to learn how to use coding in a window

Reply With Quote
  #2  
Old May 28th, 2008, 08:39 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Ok, relax We'll help you. But your post is a bit confusing. What is the real problem? Parts of the code you posted should not even compile, so is it a compile problem or a runtime problem. Which compiler are you using? (I already inferred you are on windows..)

One small thing: the ifstream constructor and ifstream.open() do not take strings as their argument but const char*. So you should do this:
string s = "something";
ifstream ifs( s.c_str() );

Post one piece of code, the one you have a problem with and tell me what the problem is. If it does not compile tell me which line and the exact message. If it is a runtime problem tell me what you expect and what the real output is.

For what it's worht I think your approach with FindFirstFile is almost right so you're nearly there.. I usually have some problems in msvc2005 with the fact that by default the wide char versions get used.
__________________
There is no such thing as C/C++, you either program C or C++

Last edited by Icon : May 28th, 2008 at 08:45 AM.

Reply With Quote
  #3  
Old May 28th, 2008, 08:57 AM
MrSquiggles MrSquiggles is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 MrSquiggles User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 54 sec
Reputation Power: 0
"One small thing: the ifstream constructor and ifstream.open() do not take strings as their argument but const char*. ..." which would be why at least of my techniques failed horribly. So here is a question, and please be gentle, if I create a loop with ifstream(chars) than I should able to do this (trying this right now)?

I spent about too many hours researching this but certainly helped raise my awareness of syntax on the smallest things and appreciating websearching hehehe.


This will list every .TXT file in the c: drive, in the while statement is where I assume the opening occurs so I can finally do some in>> statements!

Quote:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inputFile;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile("C:\\*.txt", &FindData);
cout << FindData.cFileName << endl;

while (FindNextFile(hFind, &FindData))
{
inputFile.open ("sometextfile.txt"); //I can only open single files up
//would like to open all one by one
}

inputFile.close();
system("pause");
return 0;
}



PS: I'm going to try to ifstream now with a loop, i need emphasize gratefulness in aid on this one, that above comment = useful knowing strings are out of question into the ();

I use Dev C++, realized a new issue of how to get chars from this and I read there is a limit of character intake so hope thats not a biggie.

Last edited by MrSquiggles : May 28th, 2008 at 09:14 AM. Reason: using quotes now

Reply With Quote
  #4  
Old May 28th, 2008, 09:16 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Ok, I quickly put this together for you. I should have put more comments. (I am at work so cannot get into it to deep). See if that works for you. If you have questions about it, dont hesitate to ask.

cpp Code:
Original - cpp Code
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     string path = "c:\\test\\";
  12.     string searchPattern = "*.txt";
  13.     string fullSearchPath = path + searchPattern;
  14.  
  15.     WIN32_FIND_DATA FindData;
  16.     HANDLE hFind;
  17.  
  18.     hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );
  19.  
  20.     if( hFind == INVALID_HANDLE_VALUE )
  21.     {
  22.         cout << "Error searching directory\n";
  23.         return -1;
  24.     }
  25.  
  26.     do
  27.     {
  28.         string filePath = path + FindData.cFileName;
  29.         ifstream in( filePath.c_str() );
  30.         if( in )
  31.         {
  32.             // do stuff with the file here
  33.         }
  34.         else
  35.         {
  36.             cout << "Problem opening file " << FindData.cFileName << "\n";
  37.         }
  38.     }
  39.     while( FindNextFile(hFind, &FindData) > 0 );
  40.  
  41.     if( GetLastError() != ERROR_NO_MORE_FILES )
  42.     {
  43.         cout << "Something went wrong during searching\n";
  44.     }
  45.  
  46.     system("pause");
  47.     return 0;
  48. }
  49.  
  50. // Note that you can use GetLastError() to get better error information for the
  51. // WinAPI calls but I did not want to complicate the example
  52.  

Reply With Quote
  #5  
Old May 28th, 2008, 09:25 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Note that I changed the path to "c:\\test\\" so you'll have to correct accordingly. I changed the while into a do-while since FindFirstFile already finds a file, otherwise you would be skipping the first file every time. You do not necessarily need to use do-while though.

I moved the input file into the while loop since that is the place where you use it. At the end of the scope, in this case every iteration, the ifstream will automatically be destroyed and will close the file, so no need for close().

"useful knowing strings are out of question into the ();" I dont know what you mean by (); but you can pass strings into functions of course! Just not in the ones that you were using.

Reply With Quote
  #6  
Old May 28th, 2008, 09:43 AM
MrSquiggles MrSquiggles is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 MrSquiggles User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 54 sec
Reputation Power: 0
THANK YOU!! I stayed up til 3AM several times in absolute frustration and it seemed so basic and many times I cussed up a storm. My only itty ever so negligible comment which I should be shot on sight for mentioning is that your compiler numbered each line grrr, I was like I know I'm going to have to delete them because I just am going to, but if this compiles bad I'm going to be dropping a few f bombs. Also, it sent me to one of the errors, but I caught it immediately and put a Pause to find it was the location. I THANK YOU SO MUCH, I actually got a migraine over this and I rarely get headaches. I'm give kudos one more time in a few minutes but brother in iraq is buzzing me.

Reply With Quote
  #7  
Old May 28th, 2008, 11:38 AM
MrSquiggles MrSquiggles is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 MrSquiggles User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 54 sec
Reputation Power: 0
A+

I'm doing an analytical thing of exponentially large text files for a big personal project I'm hoping to do some statistical observation and it was just impossible for me to do these 1 by 1 by renaming manually or typing them all in source.

I'll never need to know how to do this which kind of infuriated me all along (I'm an absolute C++ novice and might improve a little with time, but it's outta my league and I knew it long ago), but I needed to have this code like I have to have firefox.

Bravo sir you saved me from multiple nights of cursing of bad moods until someone just handed it to me plain text. Like this is outta my park, still is, but I just needed this particular script I'll pass the kindness on in my own way.


THANK YOU!

Reply With Quote
  #8  
Old February 17th, 2009, 03:22 PM
Judicator Judicator is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 2 Judicator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 37 sec
Reputation Power: 0
MR Squiggles, I tried compiling your code and got the following:

error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

I tried casting the parameter to LPCWSTR but then i get garbage.

Reply With Quote
  #9  
Old February 17th, 2009, 03:29 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
You've got three options:
- Start using wide strings (i.e., std::wstring, wchar_t*, L"literal" and all that)
- Use TChars, these are windows typedefs (+ utility functions) that will either be ansi characters or wide (~unicode) characters depending on certain #defines
- Change your project settings to use ansi chars. In visual studio (which I am guessing you are using?) there is a setting for unicode support for your project. In your case it is on that is why all the string functions end in 'W'. Each string function in windows is actually a macro that either turns into the same function ending in 'A' for ansi characters or 'W' for wide characters (i.e., FindFirstFileA and FindFirstFileW, FindFirstFile does not actually exist)

Reply With Quote
  #10  
Old February 17th, 2009, 05:17 PM
Judicator Judicator is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 2 Judicator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 37 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
You've got three options:
- Start using wide strings (i.e., std::wstring, wchar_t*, L"literal" and all that)
- Use TChars, these are windows typedefs (+ utility functions) that will either be ansi characters or wide (~unicode) characters depending on certain #defines
- Change your project settings to use ansi chars. In visual studio (which I am guessing you are using?) there is a setting for unicode support for your project. In your case it is on that is why all the string functions end in 'W'. Each string function in windows is actually a macro that either turns into the same function ending in 'A' for ansi characters or 'W' for wide characters (i.e., FindFirstFileA and FindFirstFileW, FindFirstFile does not actually exist)


I tried option #3. It spits out what I'd guess is a memory location? 0012FC9C. I'm using this code:

EDIT: and yes I'm using Visual Studio 2008

Code:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inputFile;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile((LPCWSTR)"C:\\*.txt", &FindData);
cout << FindData.cFileName << endl;

while (FindNextFile(hFind, &FindData))
{
	cout << FindData.cFileName << endl;
}

inputFile.close();
return 0;
} 

Reply With Quote
  #11  
Old February 18th, 2009, 01:36 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
The only thing that seems suspicious at a cursory glance is the cast, remove the C-style cast to LPCWSTR.

LPCWSTR means more or less long pointer to a constant wide string. You are not using widestrings anymore, although I am not sure this will fix you problem.

If that does not help then I suggest you check the return values of your system calls. http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx

Reply With Quote
  #12  
Old May 28th, 2010, 11:23 AM
cg9289 cg9289 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2010
Posts: 7 cg9289 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 10 m 14 sec
Reputation Power: 0
Icon,

How would you like to be cited as Icon when we referance you?

Secondly I am running the same basic skelton of your code, and am running into an error at this point

Code:
    do 
    {
        // ERROR LINE ||||||
        string filePath = path + FindData.cFileName;     // concatanates our original path with the new file name
        ifstream in( filePath.c_str());         // filePath is a str c_str makes a character array and returns a pointer 

        if( in )
        {
            int x,y,z;
            while(x>>y)
            {
                z= x+y;
            }
            cout << z << endl;
        }


it outputs this error for the first line of the do statement

error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>'

any ideas?

Thanks

Steave

Reply With Quote
  #13  
Old June 3rd, 2010, 10:12 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
That error makes no sense to me. I'm sure something else is off, besides just that line.. Which compiler/version are you using?

Try this: string filePath( path + string(FindData.cFileName) );

and see what happens

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ opening all .txt files in a folder one by one


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap