鱼C论坛

 找回密码
 立即注册
查看: 2683|回复: 4

文件操作有关

[复制链接]
发表于 2014-1-20 17:26:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是从键盘输入一个字符串,将小写字母转换为大写字母然后在保持到名文test.txt的文件中。。。这个代码最后操作只能将字符串的第一个字母转换为大写。。。我找了很久也不知道问题出在哪。。求大神帮忙啊!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


main()
{
    FILE *fp;

    char str[10],filename[20];
    int i=0;

    if((fp=fopen("test.txt","w"))==NULL)
    {
        printf("cannot open this file!\n");
        exit(0);
    }
    printf("please enter the string and end with !:");

    gets(str);
    while(str[i]!='!')
    {
        if(str[i]>='a'&&str[i]<='z')
        {
            str[i]=str[i]-32;
            fputc(str[i],fp);
            i++;
        }
        fclose(fp);

        if((fp=fopen("test.txt","r"))==NULL)
        {
            printf("cannot open this file\n");
            exit(0);
        }

        fgets(str,strlen(str)+1,fp);
        printf("%s\n",str);
        fclose(fp);
        return 0;
    }
}



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-1-20 20:34:37 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.         FILE *fp;
  7.        
  8.         char str[10],filename[20];
  9.         int i=0;
  10.        
  11.         if((fp=fopen("test.txt","w"))==NULL)
  12.         {
  13.                 printf("cannot open this file!\n");
  14.                 exit(0);
  15.         }
  16.         printf("please enter the string and end with !:");
  17.        
  18.         gets(str);
  19.         while(str[i]!='!')
  20.         {
  21.                 if(str[i]>='a'&&str[i]<='z')
  22.                 {
  23.                         str[i]=str[i]-32;
  24.                         fputc(str[i],fp);                       
  25.                 }
  26.                 i++;
  27.         }
  28.         fclose(fp);
  29.        
  30.         if((fp=fopen("test.txt","r"))==NULL)
  31.         {
  32.                 printf("cannot open this file\n");
  33.                 exit(0);
  34.         }
  35.        
  36.         fgets(str,strlen(str)+1,fp);
  37.         printf("%s\n",str);
  38.         fclose(fp);
  39.         return 0;       
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-1-20 23:14:44 | 显示全部楼层
你的while循环搞那么长,文件都关闭了还怎么转换。反大括号搞到i++;后面就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-21 09:23:09 | 显示全部楼层

十分感谢啊!!真的是这样也!!我太弱智了。。哎
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-1-21 12:27:50 | 显示全部楼层
Jane爱蓝黑墨水 发表于 2014-1-21 09:23
十分感谢啊!!真的是这样也!!我太弱智了。。哎

出错是好事,最怕没有错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 22:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表