|
|
I do not know how to code to read all files of *.txt in a directory without putting
all the names of these file into a file.
If my program can read all the files of *.txt automatically (without knowing how many and exact file name of each) in a directory and pick up some data
in each file (all these files have the same format).
I need some code fragments.
Thanks a a lots.
|
|
|
|
|
you want FindFirstFile() and FindNextFile().
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks Luc.
At present, I have to prepare a file of the file name to let the program to read this file first.
Then the program to read and process each of these files.
the file: file_inf contains:
070924104834.TXT
070924104328.TXT
070924104457.TXT
Could you please give me a few lines of code
Thanks
|
|
|
|
|
No no, you make the effort; and when stuck, ask specific questions. Then we'll help you.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
FindFirstFile(para1,para2) takes two parameters. My question is: what are types for those parameters ?
I tried to find these functions from books, but not find.
Please help
|
|
|
|
|
Try this:
http://www.google.com[^]
The very first hit for "FindFirstFile" brings you to the MSDN documentation for this function, even with an example.
|
|
|
|
|
Followed the example you suggested, I put the following code and compiled, but I can not get
the file list in the directory where the program is in. Particularly, I think I did not put
correctly: hFind = FindFirstFile(*.txt, &FindFileData);
Please advise me and thanks alots.
---------------------------------------
#define BUFSIZE MAX_PATH
WIN32_FIND_DATA FindFileData;
LPTSTR DirSpec;
//
HANDLE hFind = INVALID_HANDLE_VALUE;
CString fileName[1000];
int i=0;
DirSpec = (LPTSTR) malloc (BUFSIZE);
//
rr.DoModal();
UpdateData();
//
hFind = FindFirstFile(*.txt, &FindFileData);
fileName[0]=FindFileData.cFileName;
//
while (FindNextFile(hFind, &FindFileData) != 0)
{
i=+1;
fileName[i]=FindFileData.cFileName;
}
int nfile=i;
FindClose(hFind);
//
fprintf (f_ptr1, "%i\n", nfile);
//
for (int ii=0;ii
|
|
|
|
|
Please look here[^] for help.
Regards,
Paresh.
|
|
|
|
|
You can use of FindFirstFile that it has two parameter for first parameter you can use of "*.txt" and for read files you can use of CFile Class,did you try it?
|
|
|
|
|
Hamid,
Thanks. Please check my code fragments to collect all files of *.txt in the current directory where the program
resides. The following code is not working. It compiles, but did not pickup any files of *.txt in the directory.
I do not know why and where is the problem ?
Thanks
//
hFind = FindFirstFile("*.txt", &FindFileData);
fileName[0]=FindFileData.cFileName;
//
while (FindNextFile(hFind, &FindFileData) != 0)
{
i=+1;
fileName[i]=FindFileData.cFileName;
}
int nfile=i;
FindClose(hFind);
|
|
|
|
|
Before of using FindFirstFile use of setCurrentDirectory and enter path.
|
|
|
|
|
Hello,
I have a client and server started by a 3rd process. The 3rd process creates some pipes (via CreatePipe) and hands the handles over to the client and server processes. Client and server are able to send each other messages. My main problem now is how to detect if the connection between both dropped. With sockets that's easy, but I haven't found a way with pipes so far.
My current approach is like the following
3rd process:
CreatePipe(&readEnd, &writeEnd)
spawnServer( readEnd )
spawnClient( writeEnd )
server:
WriteFile( writeEnd, "someData" )
Sleep( 10 secs )
CloseHandle(writeEnd)
client:
while (true)
{
bytesToRead = PeekNamedPipe( readEnd )
if (bytesToRead == 0) continue;
data = ReadFile( readEnd, bytesToRead )
print(data)
}
Now when the server drops the connection the client will never know because PeekNamedPipe will always return 0 bytes to read even after the writeEnd's handle was closed. So I tried an alternative approach:
client:
SetNamedPipeHandleState( readEnd, PIPE_NOWAIT, NULL, NULL )
while (true)
{
data = ReadFile( readEnd, 1 )
print(data)
}
But after all the data is read this gives me an error 232: ReadFile: Pipe is just being closed. The pipe is not supposed to close after it read all data! It should wait there for new data.
So how can I detect if one end of a pipe has dropped the connection?
-Matthias
|
|
|
|
|
Why are you using a busy loop to wait for data on the client?
This is a terribly inefficient use of CPU cycles.
If you instead wait on ReadFile(), do you get a ERROR_BROKEN_PIPE
error when the other end disconnects?
while (true)
{
data = ReadFile( readEnd, bytesToRead )
if error and error==ERROR_BROKEN_PIPE then break
print(data)
}
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
The busy loop is just for testing right now. The code is part of a larger project I am working on. I couldn't find the problem with pipes so I made a small isolated testcase.
If I wait on ReadFile it just sits there waiting forever. For example if I do something like
while (true)
{
data = ReadFile( readEnd, 1 )
if error and error==ERROR_BROKEN_PIPE then break
print(data)
}
then it will print the data the server has written and stop printing anything. Even after the 3rd process and the server which wrote the data are long gone the client is still waiting on the ReadFile call. It just never returns.
Any ideas?
-Matthias
|
|
|
|
|
I found out the problem. I wasn't closing the handles to the write end of the pipe cleanly. Wasn't passing them properly from 3rd process to server process. After I fixed that it works now and calls like PeekNamedPipe and ReadFile actually fail
Thanks for your help!
-Matthias
|
|
|
|
|
Cool
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
In stdafx.h, I wrote
<br />
#include <Guilib.h><br />
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF","adoEOF")<br />
There are lots of enum redefinitions.
How to correct it?
Thanks.
-- modified at 21:07 Thursday 22nd November, 2007
|
|
|
|
|
I need to be able to take the collected data from several vectors and place them inside a "Main Vector" that will hold all of the data collected.
Here's some code:
class CMyTrailer {
public:
// Varable
CString m_csData;
CMyTrailer();
~CMyTrailer();
};
typedef std::vector<CMyTrailer> ITEM;
typedef std::vector<CMyTrailer> MFG;
typedef std::vector<CMyTrailer> ID;
typedef std::vector<CMyTrailer>::iterator ITItem;
typedef std::vector<CMyTrailer>::iterator ITMFG;
typedef std::vector<CMyTrailer>::iterator ITID;
vector <CMyTrailer> m_Item;
vector <CMyTrailer> m_MFG;
vector <CMyTrailer> m_ID;
// iterators:
vector<CMyTrailer>::iterator m_ITItem;
vector<CMyTrailer>::iterator m_ITMFG;
vector<CMyTrailer>::iterator m_ITID;
Here's my code to copy it:
void CTrlMasterDlg::AddVecItem(const ITEM& vec)
{
vector<CMyTrailer>::iterator m_ITItemvec;
m_ITItem = m_Item.end();
m_ITItemvec = vec.begin();
while(m_ITItemvec != vec.end())
{
m_Item.pushback(vec.);
m_ITItemvec++;
}
}
I get this error on compile:
C2039: 'pushback' : is not a member of 'std::vector<_Ty>'
1> with
_Ty=CMyTrailer
Any ideas on how to solve this?
A C++ programming language novice, but striving to learn
|
|
|
|
|
vector <CMyTrailer> m_Item;
vector <CMyTrailer> m_MFG;
...fill...
m_Item.insert( m_Item.end(), m_MFG.begin(), m_MFG.end());
BTW.
the name of the function is push_back
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
but for this, you have to iterate yourself thru the vector to be copied...
STL provide very powerful algorithms. check my other post.
|
|
|
|
|
std::vector<int> v1;
std::vector<int> v2;
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
|
|
|
|
|
How exactly does your code work? I'm not too famular with vector(S) or the STL in general. Will std::copy copy anything or is it just for vector(s)? The way you presented: std::copy(v1.begin(), v1.end(), std::back_inserter(v2)); I thinkl thats what I'm looking for; but I'm not sure. I want the (in your example) v1 vector loaded at the end of it's current data: with the v2 vector (beginning at the front of v2(begin()) to v2's end (end()).
Also How do I show the data in each CMyTrailer::m_csData ? (console example is sufficient). I want to verify that everything is doing what I want it to do before I placed it in a ListBox in my program.
Thanks,
A C++ programming language novice, but striving to learn
-- modified at 14:12 Thursday 22nd November, 2007
What I mean, is I don't know how to extract CMyTrailer::m_csData from each iteration of the vector.
|
|
|
|
|
ok, i'm detailing a bit more.
the STL is a set of classes, all in the standard of C++.
some of the classes are containers (vector is one of these).
some other classes are called algorithms. they are designed to do common actions on the containers. copying a part of a container into another is one of those common things we can except of it.
if you have a vector v1, which contains say {4, 7, -1} and a vector v2, which has {2, 9}, then you just copy the line of code just like I did and after that, v2 will contain {2, 9, 4, 7, -1}.
actually, v1 is copied entierely because i told the copy() algorithm to do so. i said to start the copy from the first element of v1 (v1.begin()) and to copy to the end (v1.end()).
also, the elements of v1 are copied at the end of v2 because I use the back_inserter() alorithm, which is designed to insert at the back.
|
|
|
|
|
void CTrlMasterDlg::AddVecItem(const ITEM& vec)
{
// vector to be copied: iterator
//vector<CMyTrailer>::iterator m_ITItemvec;
//vector to hold all copied data: id m_Item
// set iterator at back of m_Item:
m_ITItem = m_Item.end();
// set vector to be copied at the front of it's vector:
m_ITItemvec = vec.begin();
while(m_ITItemvec != vec.end())
{
/*
m_Item.pushback(vec.);
m_ITItemvec++;
*/
// Is this right?
//std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
std::copy(vec.begin(),vec.end(),back_insertor(m_Item));
}
}
So using my vectors: if I want everything from from vec to go into m_Item vector start at m_Itetm's last data(MyTrailer), is the above code correct? If not would you show me?
A C++ programming language novice, but striving to learn
|
|
|
|