我試圖做3兩件事:打印和保存的.txt
- 加載.txt文件
- 打印文件到控制檯的內容。
- 用另一個名字再次保存。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char text[500]; /* Create a character array that will store all of the text in the file */
char line[100]; /* Create a character array to store each line individually */
int inpChar;
FILE *file; /* Create a pointer to the file which will be loaded, to allow access to it */
char fileName[100]; /* Create a character array to store the name of the file the user want to load */
do {
printf("enter menu: [l]oad - [s]ave - [p]rint\n");
scanf("%c", &inpChar);
}
while((inpChar != 'l') && (inpChar != 's') && (inpChar !="p"));
if((inpChar == 'l'))
{
printf("Enter the name of the file containing ship information: ");
}
scanf("%s", fileName);
/*Try to open the file specified by the user. Use error handling if file cannot be found*/
file = fopen(fileName, "r"); /* Open the file specified by the user in 'read' mode*/
if(file == NULL){
printf("The following error occurred.\n");
}
else {
printf("File loaded. \n"); /* Display a message to let the user know
* that the file has been loaded properly */
}
do {
printf("enter menu: [l]oad - [s]ave - [p]rint\n");
scanf("%c", &inpChar);
}
while((inpChar != 'l') && (inpChar != 's') && (inpChar !='p'));
if((inpChar == 'p'))
{
file = fopen(fileName, "r");
fprintf(file, "%s", line);
fclose(file);
}
return 0;
}
我缺少的控制檯面板上的打印文本;它沒有工作,代碼中沒有保存選項。我該怎麼辦?
'file = fopen(fileName,「r」); if(file == NULL){perror(filename); }' – 2013-02-14 12:10:06
Define「it does not work」.. – KBart 2013-02-14 12:10:07
您是否可以減少代碼示例以僅包含不工作的代碼,而不會像用戶界面那樣混亂? – millimoose 2013-02-14 12:15:07