#define _CRT_SECURE_NO_WARNINGS // ワーニングを無効にする #include #include #define MAX_CHAR_PER_LINE 256 int main() { FILE* inputFile, * outputFile; char tempFileName[] = "temp.txt"; char inputFileName[] = "input.txt"; char outputFileName[] = "output.txt"; char newLine[] = "This is the new first line.\n"; char buffer[MAX_CHAR_PER_LINE]; int lineNumber = 0; // Open the input file for reading if (fopen_s(&inputFile, inputFileName, "r") != 0) { perror("Error opening the file"); return 1; } // Open the temporary file for writing if (fopen_s(&outputFile, tempFileName, "w") != 0) { perror("Error opening temporary file"); return 1; } // Write the new first line to the temporary file fputs(newLine, outputFile); // Read each line from the input file and write to the temporary file, excluding the line to be deleted while (fgets(buffer, MAX_CHAR_PER_LINE, inputFile) != NULL) { lineNumber++; // Skip the line to be deleted (in this example, we delete line 2) if (lineNumber == 2) { continue; } fputs(buffer, outputFile); } // Close the input and temporary files fclose(inputFile); fclose(outputFile); // Remove the original file if (remove(inputFileName) != 0) { perror("Error deleting the file"); return 1; } // Rename the temporary file to the original file name if (rename(tempFileName, outputFileName) != 0) { perror("Error renaming the file"); return 1; } printf("Operation successful.\n"); return 0; }