1. 程式人生 > >文件讀取的問題----末尾多一個空格

文件讀取的問題----末尾多一個空格

src win int 多一個空格 new include etc 問題 末尾

#include "stdafx.h"
#include <stdio.h> 
#include<Windows.h>
int main() {
    char* filename=new char[1024];
    filename = "E:\\下載文件\\vs2016\\first\\first\\mingwen.txt";//任意一個自己有該txt文件的路徑
    FILE *fp;
    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("無法打開此文件\n");
        exit(
0); } char *miwen = new char[1024]; int i = 0; while (!feof(fp)) { miwen[i++] = fgetc(fp); } //i--; miwen[i] = ‘\0‘; printf("%s",miwen); getchar(); return 0; }

在getchar();處加斷點進行調試,得

技術分享圖片

#include "stdafx.h"
#include <stdio.h> 
#include<Windows.h>
int
main() { char* filename=new char[1024]; filename = "E:\\下載文件\\vs2016\\first\\first\\mingwen.txt";//任意一個自己有該txt文件的路徑 FILE *fp; if ((fp = fopen(filename, "r")) == NULL) { printf("無法打開此文件\n"); exit(0); } char *miwen = new char[1024]; int i = 0; while (!feof(fp)) { miwen[i
++] = fgetc(fp); } i--; miwen[i] = ‘\0‘; printf("%s",miwen); getchar(); return 0; }

在getchar();處加斷點進行調試,得

技術分享圖片

文件讀取的問題----末尾多一個空格