鱼C论坛

 找回密码
 立即注册
查看: 1067|回复: 6

fputc输出到文件内容,与printf输出到屏幕不一样的问题

[复制链接]
发表于 2022-2-19 11:04:53 | 显示全部楼层 |阅读模式

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

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

x

主要问题点:
ch =(char)tmp ;
printf("%c",ch);
fputc(ch,file4);
输出是这样的,也是我想要的
heroname:后羿
heroid:1
heroAtk:1000
herophy:200
herotype:射手
请输入你要查询的内容(退出查询请输入2):
但是写入文件以后是这样的
heroname:后羿

heroid:1

heroAtk:1000

herophy:200

herotype:射手
多了好几个换行



求助求助,感谢感谢感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-19 12:14:30 | 显示全部楼层
发完整代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-19 14:56:00 | 显示全部楼层
char ch=0;
          srand(time(NULL)); 
          FILE *file3 =fopen("加密文件.txt","w+");
        while(1)
        {
                ch=fgetc(file2);
                
                if(!feof(file2))
                {
                        short tmp = (short)ch;
                        tmp = tmp << 4;
                //        printf("%d\n",tmp);
                        tmp = tmp | 0x8000 ;
                //        printf("%d\n",tmp);
                        tmp = tmp + rand()%16;
                //        printf("%d\n",tmp);
                        fprintf(file3,"%hd",tmp);
                }
                else
                break;
        }
        fclose(file3);
        file3 = NULL;
        
        //---------------------------------------------------------------------
        //文件解密
//        1000 0100 1100 0110  << 1
//        0000 1001 1000 1100         >> 5
//        0000 0000 0100 1100 
        
        file3 = fopen("加密文件.txt","r");
        if(!file3)
        {
                perror("");
                return -1;
        }
        FILE *file4 = fopen("解密文件.txt","w+");
        if(!file4)
        {
                perror("");
                return -1;
        }
        while(!feof(file3))
        {
                short tmp;
                fscanf(file3,"%hd",&tmp);
                        tmp = tmp << 1;
                        tmp = tmp >> 5;        
                        ch =(char)tmp ;
                        printf("%c",ch);
                        fputc(ch,file4);
        }
        fclose(file4);
        file4 = NULL;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-19 17:30:32 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-23 08:58:07 | 显示全部楼层


这里问题在 写入文件的内容每一行都多了一个换行


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//CR:Carriage Return,对应ASCII中转义字符\r,表示回车
//
//LF:Linefeed,对应ASCII中转义字符\n,表示换行
//
//CRLF:Carriage Return & Linefeed,\r\n,表示回车并换行
//
//众所周知,
//
//Windows操作系统采用两个字符来进行换行,即CRLF;
//
//Unix/Linux/Mac OS X操作系统采用单个字符LF来进行换行;
//
//另外,MacIntosh操作系统(即早期的Mac操作系统)采用单个字符CR来进行换行。 
int main()
{
        FILE *file1 = fopen("原文件.txt","rb");
        FILE *file2 = fopen("新文件.txt","wb+");
        if(!file1||!file2)
        {
                perror("");
                return -1;
        }
        
        char buf[1024] ="";
        while(fgets(buf,sizeof(buf),file1)!=NULL)        //获取有效信息 
        {
                //printf("[%s]\n",buf);        // 打印显示乱码 ,将txt文本编码改成 ANSI 
                for(int i=0;buf[i]!='\0';i++)
                {
                        
                         if(buf[i]==':')//将有效信息存入 新文件 
                        {
                        //fputs()可以指定输出的文件流,不会输出多余的字符;puts()只能向 stdout 输出字符串,而且会在最后自动增加换行符。
                                int n=fputs(buf,file2);        
                                
                        }        
                }
         } 
        //        fflush 强行将缓冲中的数据同步到硬盘上去
        //fflush(file2);
        //        fclose 文件关闭前,会自动将缓冲中数据同步到硬盘
          fclose(file2);
          file2 = NULL;        
          file2 = fopen("新文件.txt","rb");
          if(!file2)
        {
                perror("");
                return -1;
        }
          // 文件加密
//          0000 0000 0100 1100        << 4
//          0000 0100 1100 0000   | 1000 0000 0000 0000  将数字全置为负数 
//          1000 0100 1100 0000        + 0000~1111(0-15)
//          1000 0100 1100 0110
          
          char ch=0;
          srand(time(NULL)); 
          FILE *file3 =fopen("加密文件.txt","w+");
        while(1)
        {
                ch=fgetc(file2);
                
                if(!feof(file2))
                {
                        short tmp = (short)ch;
                        tmp = tmp << 4;
                //        printf("%d\n",tmp);
                        tmp = tmp | 0x8000 ;
                //        printf("%d\n",tmp);
                        tmp = tmp + rand()%16;
                //        printf("%d\n",tmp);
                        fprintf(file3,"%hd",tmp);
                }
                else
                break;
        }
        fclose(file3);
        file3 = NULL;
        
        //---------------------------------------------------------------------
        //文件解密
//        1000 0100 1100 0110  << 1
//        0000 1001 1000 1100         >> 5
//        0000 0000 0100 1100 
        
        file3 = fopen("加密文件.txt","r");
        if(!file3)
        {
                perror("");
                return -1;
        }
        FILE *file4 = fopen("解密文件.txt","w");
        if(!file4)
        {
                perror("");
                return -1;
        }
        while(!feof(file3))
        {
                short tmp;
                fscanf(file3,"%hd",&tmp);
                        tmp = tmp << 1;
                        tmp = tmp >> 5;        
                        ch =(char)tmp ;
                        printf("%c",ch);
                        fputc(ch,file4);
        }
        fclose(file4);
        file4 = NULL;
        //--------------------------------------------------------------------------
        //        查询信息 
        char newbuf[1024]=" ";
        while(1)
        {
                printf("请输入你要查询的内容(退出查询请输入2):");
                fgets(newbuf,sizeof(newbuf),stdin);
                newbuf[strlen(newbuf)-1]='\0';
        //        printf("[%s]",newbuf);
                int i=1;
                fseek(file2,0,SEEK_SET);
                if(newbuf[0]=='2')
                {
                        break;
                }         
                while(fgets(buf,sizeof(buf),file2)!=NULL)
                {
                
                        if(!strncmp(newbuf,buf,strlen(newbuf)))
                        {
                                i=0;
                                printf("%s",(char *)buf+strlen(newbuf)+1);
                                break;
                        }
                }
                if(i)
                printf("抱歉,未查询到!\n");
        }
        fclose(file1);
        fclose(file2); 
        system("pause"); 
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-23 12:05:22 | 显示全部楼层
想入门的新人 发表于 2022-2-23 08:58
这里问题在 写入文件的内容每一行都多了一个换行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// CR:Carriage Return,对应ASCII中转义字符\r,表示回车
// LF:Linefeed,对应ASCII中转义字符\n,表示换行
// CRLF:Carriage Return & Linefeed,\r\n,表示回车并换行
// 众所周知,
// Windows操作系统采用两个字符来进行换行,即CRLF;
// Unix/Linux/Mac OS X操作系统采用单个字符LF来进行换行;
// 另外,MacIntosh操作系统(即早期的Mac操作系统)采用单个字符CR来进行换行。

int main() {
    FILE *file1 = fopen("原文件.txt", "rb");
    FILE *file2 = fopen("新文件.txt", "wb+");
    if(!file1 || !file2) {
        perror("");
        return -1;
    }

    //char buf[1024] = "";
    char buf[1024];     // 不需要初始化吧?
    while(fgets(buf, sizeof(buf), file1) != NULL) //获取有效信息
    {
        // printf("[%s]\n",buf);        // 打印显示乱码 ,将txt文本编码改成 ANSI
        for(int i = 0; buf[i] != '\0'; i++) {
            if(buf[i] == ':') //将有效信息存入 新文件
            {
                // fputs()可以指定输出的文件流,不会输出多余的字符;puts()只能向
                // stdout 输出字符串,而且会在最后自动增加换行符。
                //int n = fputs(buf, file2);    // 这是在做什么?
            }
        }
    }
    //        fflush 强行将缓冲中的数据同步到硬盘上去
    // fflush(file2);
    //        fclose 文件关闭前,会自动将缓冲中数据同步到硬盘
    fclose(file2);

    // 下面的代码没法看了,因为没看懂上面的代码,上面的代码在做什么?
    // 把代码删减一下,突出你的问题


    //file2 = NULL;
    file2 = fopen("新文件.txt", "rb");
    if(!file2) {
        perror("");
        return -1;
    }
    // 文件加密
    //          0000 0000 0100 1100        << 4
    //          0000 0100 1100 0000   | 1000 0000 0000 0000  将数字全置为负数
    //          1000 0100 1100 0000        + 0000~1111(0-15)
    //          1000 0100 1100 0110

    char ch = 0;
    srand(time(NULL));
    FILE *file3 = fopen("加密文件.txt", "w+");
    while(1) {
        ch = fgetc(file2);

        if(!feof(file2)) {
            short tmp = (short)ch;
            tmp = tmp << 4;
            //        printf("%d\n",tmp);
            tmp = tmp | 0x8000;
            //        printf("%d\n",tmp);
            tmp = tmp + rand() % 16;
            //        printf("%d\n",tmp);
            fprintf(file3, "%hd", tmp);
        } else
            break;
    }
    fclose(file3);
    file3 = NULL;

    //---------------------------------------------------------------------
    //文件解密
    //        1000 0100 1100 0110  << 1
    //        0000 1001 1000 1100         >> 5
    //        0000 0000 0100 1100

    file3 = fopen("加密文件.txt", "r");
    if(!file3) {
        perror("");
        return -1;
    }
    FILE *file4 = fopen("解密文件.txt", "w");
    if(!file4) {
        perror("");
        return -1;
    }
    while(!feof(file3)) {
        short tmp;
        fscanf(file3, "%hd", &tmp);
        tmp = tmp << 1;
        tmp = tmp >> 5;
        ch = (char)tmp;
        printf("%c", ch);
        fputc(ch, file4);
    }
    fclose(file4);
    file4 = NULL;
    //--------------------------------------------------------------------------
    //        查询信息
    char newbuf[1024] = " ";
    while(1) {
        printf("请输入你要查询的内容(退出查询请输入2):");
        fgets(newbuf, sizeof(newbuf), stdin);
        newbuf[strlen(newbuf) - 1] = '\0';
        //        printf("[%s]",newbuf);
        int i = 1;
        fseek(file2, 0, SEEK_SET);
        if(newbuf[0] == '2') {
            break;
        }
        while(fgets(buf, sizeof(buf), file2) != NULL) {
            if(!strncmp(newbuf, buf, strlen(newbuf))) {
                i = 0;
                printf("%s", (char *)buf + strlen(newbuf) + 1);
                break;
            }
        }
        if(i) printf("抱歉,未查询到!\n");
    }
    fclose(file1);
    fclose(file2);
    //system("pause");      // 不是所有编译环境都能用这个
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-23 13:44:16 | 显示全部楼层


                               
登录/注册后可看大图

                               
登录/注册后可看大图

                               
登录/注册后可看大图


原文件经过加密解密后多了几行换行
我使用的是DEV c++这个软件 system(“pause”);这个可以用没问题
感谢帮助,感谢感谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
int main()
{
        FILE *file1 = fopen("原文件.txt","rb");
        FILE *file2 = fopen("新文件.txt","wb+");
        if(!file1||!file2)
        {
                perror("");
                return -1;
        }
        
        char buf[1024] ="";
        while(fgets(buf,sizeof(buf),file1)!=NULL)        //获取有效信息 
        {
                for(int i=0;buf[i]!='\0';i++)
                {        
                         if(buf[i]==':')//将有效信息存入 新文件 
                        {
                                int n=fputs(buf,file2);                
                        }        
                }
         } 
          fclose(file2);
          file2 = NULL;        
          file2 = fopen("新文件.txt","rb");
          if(!file2)
        {
                perror("");
                return -1;
        }
          // 文件加密
//          0000 0000 0100 1100        << 4
//          0000 0100 1100 0000   | 1000 0000 0000 0000  将数字全置为负数 
//          1000 0100 1100 0000        + 0000~1111(0-15)
//          1000 0100 1100 0110
          
          char ch=0;
          srand(time(NULL)); 
          FILE *file3 =fopen("加密文件.txt","w+");
        while(1)
        {
                ch=fgetc(file2);
                
                if(!feof(file2))
                {
                        short tmp = (short)ch;
                        tmp = tmp << 4;
        
                        tmp = tmp | 0x8000 ;
        
                        tmp = tmp + rand()%16;
        
                        fprintf(file3,"%hd",tmp);
                }
                else
                break;
        }
        fclose(file3);
        file3 = NULL;
        
        //---------------------------------------------------------------------
        //文件解密
//        1000 0100 1100 0110  << 1
//        0000 1001 1000 1100         >> 5
//        0000 0000 0100 1100 
        
        file3 = fopen("加密文件.txt","r");
        if(!file3)
        {
                perror("");
                return -1;
        }
        FILE *file4 = fopen("解密文件.txt","w");
        if(!file4)
        {
                perror("");
                return -1;
        }
        while(!feof(file3))
        {
                short tmp;
                fscanf(file3,"%hd",&tmp);
                        tmp = tmp << 1;
                        tmp = tmp >> 5;        
                        ch =(char)tmp ;
                        printf("%c",ch);
                        fputc(ch,file4);
        }
        fclose(file4);
        file4 = NULL;

        fclose(file1);
        fclose(file2); 
        system("pause"); 
        return 0;
}



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-10 20:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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