WinInet FTP批量文件列表异常:仅显示前91个文件问题求助
Hey there, let's tackle this frustrating WinInet FTP issue where you're only seeing 91 files instead of the full 100+ in a folder. I've debugged similar problems before, so here are the most likely causes and actionable fixes to try:
1. You're Not Reading the Entire FTP Response (Most Common Culprit)
WinInet's InternetReadFile doesn't return the entire LIST response in a single call—especially when dealing with large directories. If your code only calls it once, you're probably truncating the data partway through the server's response.
Fix: Loop Until All Data Is Read
Replace any single InternetReadFile call with a loop that continues reading until no more bytes are received. Here's a corrected example in C++:
std::string fullResponse; char buffer[4096]; DWORD bytesRead = 0; BOOL readSuccess; do { readSuccess = InternetReadFile(hFtpFile, buffer, sizeof(buffer) - 1, &bytesRead); if (readSuccess && bytesRead > 0) { buffer[bytesRead] = '\0'; // Null-terminate the buffer fullResponse += buffer; // Append to the full response string } } while (readSuccess && bytesRead > 0); // Now parse fullResponse instead of a partial buffer
This ensures you capture every line of the LIST output, not just the first chunk.
2. Your Line Parsing Logic Is Truncating Results
Even if you read the full response, your code that splits the response into individual file lines might be missing entries. Common issues here include:
- Not handling both
CRLF(Windows-style) andLF(Unix-style) line endings - Stopping parsing early if a line doesn't match your expected format (e.g., a file with unusual characters in the name)
- Forgetting to process the final line if it doesn't end with a newline
Fix: Robust Line Splitting
Instead of relying on simple strtok calls, use a more thorough approach to split the response. For example:
size_t pos = 0; size_t nextPos; while ((nextPos = fullResponse.find('\n', pos)) != std::string::npos) { std::string line = fullResponse.substr(pos, nextPos - pos); // Trim any trailing CR if present if (!line.empty() && line.back() == '\r') { line.pop_back(); } // Process this line (parse file info) pos = nextPos + 1; } // Don't forget the final line if it doesn't end with a newline if (pos < fullResponse.size()) { std::string line = fullResponse.substr(pos); if (!line.empty() && line.back() == '\r') { line.pop_back(); } // Process the final line }
3. Check FTP Session Settings
Incorrect session configurations can cause incomplete data transfers:
- Passive Mode: Ensure you're using passive FTP (most modern servers require this). Enable it with:
DWORD passive = 1; InternetSetOption(hFtpSession, INTERNET_OPTION_USE_PASV, &passive, sizeof(passive)); - Transfer Type: LIST responses are text-based, so make sure you're using ASCII transfer mode:
FtpSetCurrentDirectory(hFtpSession, TEXT("/your/directory")); HINTERNET hFtpFile = FtpOpenFile(hFtpSession, TEXT("LIST -la"), GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
4. Debug the Raw Response
To rule out server-side issues, save the full raw response to a file and inspect it:
std::ofstream logFile("ftp_list_response.txt"); logFile << fullResponse; logFile.close();
If the log contains all 100+ files, the problem is in your parsing logic. If it only has 91, contact your server admin or check if the server has a limit on LIST response size.
内容的提问来源于stack exchange,提问作者Vas

