文件追加时出现ASCII值为-1的异常符号,求解决及替代方法
Hey there, let's tackle your file append issue and walk through solutions step by step!
What's Causing the Weird Symbol (ASCII -1)?
That "strange symbol" with ASCII value -1 is actually EOF (End of File) being written as a regular character. Here's why it happens:
- EOF is an
intvalue (-1), but if you read it into acharvariable (which is signed by default on most systems), it gets stored as -1. When you write this to the target file, it shows up as an unprintable weird symbol. - Your code also has missing pieces: no check for successful file opening, and you didn't use the correct mode to append to the target file (so you might accidentally overwrite content instead of adding to it).
Fix for Your Existing Code
Let's adjust your code to fix these issues:
#include <iostream> #include <fstream> #include <cstring> using namespace std; void AddFile(char old_name[], char old_content[]) { ofstream old_file; ifstream new_file; char new_name[255]; int input_char; // Use int to catch EOF properly cout << "Enter the file name: "; cin.ignore(); cin.getline(new_name, 255, '\n'); strcat(new_name, ".txt"); // Check if source file opens successfully new_file.open(new_name); if (!new_file.is_open()) { cerr << "Oops, couldn't open source file: " << new_name << endl; return; } // Open target file in APPEND mode (ios::app) to avoid overwriting old_file.open(old_name, ios::app); if (!old_file.is_open()) { cerr << "Oops, couldn't open target file: " << old_name << endl; new_file.close(); return; } // Read characters until EOF (use int to distinguish EOF from valid chars) new_file.seekg(0, ios::beg); while ((input_char = new_file.get()) != EOF) { old_file.put(static_cast<char>(input_char)); } // Clean up: close files new_file.close(); old_file.close(); cout << "Content appended successfully!" << endl; }
Key fixes here:
- File opening checks: We verify if each file opens correctly to avoid invalid operations.
- Append mode: Using
ios::appensures we add content to the end of the target file, not overwrite it. - EOF handling: Using
intto store the character fromget()lets us properly detect EOF without writing it to the target file.
Alternative Ways to Append Files in C++
If you want simpler or more efficient approaches, here are a few options:
1. Simplified C++ Stream Copy (C++11+)
This method uses rdbuf() to directly copy the source file's buffer to the target—no manual loop needed:
#include <fstream> #include <iostream> #include <string> void AppendFile(const std::string& src_path, const std::string& dest_path) { std::ifstream src_file(src_path); std::ofstream dest_file(dest_path, std::ios::app); if (!src_file || !dest_file) { std::cerr << "Error opening one or both files!" << std::endl; return; } // Directly copy the entire file content dest_file << src_file.rdbuf(); src_file.close(); dest_file.close(); }
2. C-Style File Operations
If you prefer C's standard library, this block-based read/write is efficient:
#include <stdio.h> #include <stdlib.h> void AppendFileC(const char* src_path, const char* dest_path) { FILE* src_file = fopen(src_path, "r"); FILE* dest_file = fopen(dest_path, "a"); // "a" = append mode if (!src_file || !dest_file) { fprintf(stderr, "Failed to open files!\n"); return; } char buffer[1024]; size_t bytes_read; // Read and write in chunks for better performance while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) { fwrite(buffer, 1, bytes_read, dest_file); } fclose(src_file); fclose(dest_file); }
3. C++17 Filesystem Library
If your compiler supports C++17, the <filesystem> library makes path handling and file checks easier:
#include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; void AppendFileFS(const fs::path& src_path, const fs::path& dest_path) { if (!fs::exists(src_path)) { std::cerr << "Source file doesn't exist!" << std::endl; return; } std::ifstream src_file(src_path); std::ofstream dest_file(dest_path, std::ios::app); if (!src_file || !dest_file) { std::cerr << "Error opening files!" << std::endl; return; } dest_file << src_file.rdbuf(); }
Quick Reminders
- Always check if files open successfully—missing permissions or non-existent files are common pitfalls.
- Never forget the append mode (
ios::appfor C++,"a"for C)—otherwise you'll overwrite your target file. - Use
intto read characters when checking for EOF to avoid writing the EOF marker into your file.
内容的提问来源于stack exchange,提问作者Doha Khaled
相关产品推荐
相关产品推荐

