我正在創建一個C程序,用於將所有小寫字母轉換爲大寫字母,反之亦然。在C程序中,我試圖從文本文件中獲取字符串並訪問每個字符。但是我的程序崩潰了。我在這裏做的錯誤是什麼?下面是我的代碼:程序崩潰時,試圖從文件中獲取字符串並訪問字符串中的單個字母
//Program to read contents from a source file and convert the lower case characters to a upper case and vice versa and print it on a target file
#include <stdio.h>
#include <stdlib.h>
void merge(FILE *fp,int n,char *fdata,FILE *fpr)
{
char change_case[1000];
int loop = 0;
while(fgets(fdata,n,fp)!=NULL)
{
fgets(fdata,n,fp);
//printf("%s",fdata);
if ((*(fdata+loop) >= 65) && (*(fdata+loop) <= 90))
{
printf("%c",*(fdata+loop));
change_case[loop] = *(fdata+loop)+32;
}
else if ((*(fdata+loop) >= 97) && ((*fdata+loop) <= 122))
{
printf("%c",*(fdata+loop));
change_case[loop] = *(fdata+loop)-32;
}
else
{
change_case[loop] = *(fdata+loop);
}
loop++;
fpr = fopen("Text File 3.dat","w");
if (fpr == NULL)
{
printf("Error in accessing/writing the target file");
}
else
{
fputs(change_case,fpr);
printf("Line written successfully : %s\n", change_case);
}
}
}
int main()
{
FILE *sfp, *tfp; //declaring a file pointer for source file and target file
char *fdata;
int n = 1000;
sfp = fopen("Text File 1.txt","r");
if (sfp == NULL)
{
printf("Error in reading the source file\n");
}
else
{
merge(sfp,n,fdata,tfp);
}
fclose(sfp);
return 0;
}
-
你可以做到這一點呢? –
歡迎來到堆棧溢出!請看[爲什麼「while(!feof(file))」總是出錯?](http://stackoverflow.com/q/5431941/2173917) –