The essence of the program is that the client sends the path to the folder, the server accepts it and, by analogy with the DIR utility, finds the folders inside it and sends them to the client, the client already displays these folders. So the problem is that when the server finds them, it sends them to the client, let's say a pack. For example:
Windows
System
Config
And when I output them via printf, only Windows
is output. This problem is that printf prints until it meets \0
and it turns out my this pack looks like this:
Windows \ 0
System \ 0
Config \ 0
The transfer programs themselves:
while (1) { // Получаем очередную команду через канал Pipe if (ReadFile(hNamedPipe, szBuf, 512, &cbRead, NULL)) { DIR *dir; struct dirent *entry; dir = opendir(szBuf); if (!dir) { perror("diropen"); exit(1); }; while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); if (!WriteFile(hNamedPipe, entry->d_name, strlen(entry->d_name) + 1, &cbWritten, NULL)); }; if (!WriteFile(hNamedPipe, "|" , strlen(szBuf) + 1, &cbWritten, NULL)); closedir(dir);
Reception:
// Передаем введенную строку серверному процессу // в качестве команды if (!WriteFile(hNamedPipe, szBuf, strlen(szBuf) + 1, &cbWritten, NULL)) break; // Получаем папку от сервера do { if (ReadFile(hNamedPipe, szBuf, 512, &cbRead, NULL)) printf("Received back: <%s>\n", szBuf); // Если произошла ошибка, выводим ее код // завершаем работу приложения else { fprintf(stdout, "ReadFile: Error %ld\n", GetLastError()); _getch(); break; } } while (szBuf != "|");
I just want to know how to read the data correctly? If you remove the addition of \ 0, that is, remove +1, then it displays everything correctly but all in one line: WindowsSystemConfig, etc.
szBuf
will never be equal to"|"
."|"
- this is a string with its address,szBuf
is another address, a buffer. And why should they coincide? Equality of the contents of strings is checked using functions likestrcmp
... - Harry