我知道这看起来很简单,但这是我一直在苦恼的程序中的一个大错误。简单地说,我想让 libcurl 从 FTP 服务器下载文件,将文本附加到该文件,然后将文件上传回服务器。我的问题是,我的程序没有将文本附加到文件中,而是覆盖了文件的内容,即使当我写入下载的文件时,我在 fopen() 中使用的是 a+ 文件操作模式。如果有人以前做过类似的事情,一些建议将不胜感激。这是相关的C代码:
void write_to_database(const char* filename, const char* string_to_write) {
FILE *file = fopen(filename, "a+");
if (file) {
printf("Current position in file: %li\n", ftell(file));
fputs(string_to_write, file);
fputs("\n", file);
fclose(file);
}
}
…
void perform_database_modification(const char* file_to_write, const char* short_database, const char* addr, const char* msg) {
strcpy(DATABASE_FILE, file_to_write);
//strcpy(DESTINATION_MUSIC_FILE, music_file);
strcpy(REMOTE_URL_HEAD, "");
strcpy(REMOTE_DATABASE_FILE, short_database);
strcpy(REMOTE_URL, addr);
strcpy(message, msg);
remove(DATABASE_FILE);
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={
DATABASE_FILE, /* name to store the file as if succesful */
NULL
};
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* specify target */
curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
printf("Remote URL: %s\n", REMOTE_URL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_database_write);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt(curl, CURLOPT_USERNAME, (char *)loginUser);
curl_easy_setopt(curl, CURLOPT_PASSWORD, (char *)loginPassword);
fprintf(stderr, "Using: %s %s\n", loginUser, loginPassword);
/* Now run off and do what you've been told! */
fflush(stdout); // Flush the buffers so we see the message immediately.
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
fprintf(stderr, "DWNLD DATAB: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
if (strcmp("Remote file not found", curl_easy_strerror(res)) != 0) // We are using this to see if remote file exists,
{ // so ignore if the error is about that.
[SBErrorNotice performSelectorOnMainThread:@selector(showError:) withObject:@(curl_easy_strerror(res)) waitUntilDone:NO]; // Show in main thread b/c NSAlert will complain otherwise.
errorOccurred = true;
}
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
}
/* always cleanup */
curl_easy_cleanup(curl);
/* Now see if the file we downloaded exists. */
if (file_exists(DATABASE_FILE))
{
printf("Database file exists.\n");
write_to_database(DATABASE_FILE, message);
} else {
printf("Music index does not exist. Uploading a blank file...\n");
create_blank_file(DATABASE_FILE);
write_to_database(DATABASE_FILE, message);
}
//write_to_database(DATABASE_FILE, message);
curl_global_cleanup();
}
任何帮助,将不胜感激。