同时在一个流中使用的fread和fwrite引起的问题 [英] Using fread and fwrite in one stream at the same time caused a problem

查看:214
本文介绍了同时在一个流中使用的fread和fwrite引起的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该计划希望从文件中读取。该文件中的内容是字符串Hello,世界。然后判断该字符串的各字符以看是否大于或等于常量字符的'e'的字符,如果字符满足条件,比在按字母顺序改变字符到previous字符(例如。 b'更改为'A','E'转变为'D')。最后,输出改变后的文件内容的画面。

问题是如何做的fwrite和FREAD工作?我为什么不能摆脱掉变量POS2简化前pression。如果有人可以帮助,非常感谢!

 的#include<&stdio.h中GT;诠释的main(){
    FILE * FP;
    个char [20];
    字符T [20];
    焦炭转移;
    INT I;
    INT POS; //存储的文件的位置,从该文件读出前
    INT POS1; //检查文件的位置
    INT POS2; //存储文件的位置,从文件中读取后,
的#pragma区域创建一个名为你好的文件,写你好,世界到该文件,关闭它
    如果((FP = FOPEN(你好,世行))== NULL)
    {
        的printf(无法打开文件\\ n);
        出口(0);
    }
    的strcpy(S,你好,世界);
    FWRITE(S,的sizeof(字符)* 20,1,FP);
    fseek的(FP,0,SEEK_SET);
    FCLOSE(FP);
的#pragma endregion创建一个名为你好的文件,写你好,世界到该文件,关闭它
区域的#pragma从名为你好,应对当前,写变化到文件的文件读取。
    如果((FP = FOPEN(你好,RB +))== NULL)
    {
            的printf(无法打开文件\\ n);
            出口(1);
    }
    I = 0;
    而(ⅰ&小于20)
    {
            //提问,该处为何不能利用fwrite的自动定位免去注释掉的语句行(即使用了pos2的语句行)。
            //这是问题所在。由于FREAD和fwrite功能可以移动的位置
            //文件,我想我可以摆脱掉下面的评论两句
            //它的变量POS2
            POS = FTELL(FP); //存储从文件中读取前的位置
            FREAD(安培;转移,的sizeof(炭),1,FP); //该文件的位置移动到下一个字符
            // POS2 = FTELL(FP); //存储从文件中读取后的位置
            POS1 = FTELL(FP);
            如果(转> ='E')//如果减去1的字符大于或等于E。
            {
                转让 - = 1;
            }
            fseek的(FP,POS,SEEK_SET); //回在字符被读取以改变字符的位置
            FWRITE(安培;转移,的sizeof(炭),1,FP); //文件的位置移动到下一个字符
            // fseek的(FP,POS2,SEEK_SET); //
            POS1 = FTELL(FP);
            我++;
    }
    fseek的(FP,0,SEEK_SET);
    FCLOSE(FP);
的#pragma endregion从名为你好,应对当前,写变化到文件的文件读取。
区域的#pragma从名为你好更改字符串的文件,输出读
    如果((FP = FOPEN(你好,RB))== NULL)
    {
            的printf(无法打开文件\\ n);
            出口(2);
    }
    FREAD(T,的sizeof(字符)* 20,1,FP);
    输出(输出为:%S \\ n,T);
//正确的输出(两个以上的句子在它被注释掉POS2):
//输出为:Hdkkn,vnqkd
//错误的输出(两个以上的句子在它POS2没有被注释掉):
//输出为:Hddddddddddddddddddd烫烫烫烫的Hello,world
    fseek的(FP,0,SEEK_SET);
    FCLOSE(FP);
的#pragma endregion从名为你好更改字符串的文件,输出读
    系统(暂停);
}


解决方案

我实际上并没有得到为什么你想在评论/出2行的地步。因为没有什么变化,你是否发表评论它们,或者加以注释。你已经得到了你的code摆脱POS2(这是你所要求的)。

因此​​,如果您使用以下code为while循环

  POS = FTELL(FP); //存储从文件中读取前的位置
FREAD(安培;转移,的sizeof(炭),1,FP);
POS1 = FTELL(FP);
如果(转> ='E')//如果减去1的字符大于或等于E。
{
    转让 - = 1;
}
fseek的(FP,POS,SEEK_SET);
FWRITE(安培;转移,的sizeof(炭),1,FP);
我++;

然后你拿的输出是:Hdkkn,vnqkd,这是预期的结果。

您也可以采取从文件中每行一个数组,并在其上​​操作,那么它写回文件。通过这种方式,它可能是更通用的,你不必用神奇数字,如20。

编辑:

我用gcc 4.5.2在我的Linux平台。我不希望在其他平台上发表评论,但正如我所说,你可以采取前行到一个缓冲区,然后操作之后,你可以把它写回。你可以尝试用whil​​e循环替换下列code:

 字符线[20] = {0};FREAD(行的sizeof(焦炭),20,FP);对于(i = 0; I<的strlen(线);我++)
{
    如果(行[I]> ='E')
        线[I] - = 1;
}fseek的(FP,0,SEEK_SET);
FWRITE(行的sizeof(炭),strlen的(线),FP);

通过这种方式,你可以摆脱许多变量。这是你的选择。

This program want to read from a file. the content in the file is the string "Hello, world". then judge each character of the string to see if the character greater than or equal to the const character 'e', if the character meet the condition, than change the character to its previous character in the alphabetical order (eg. 'b' change to 'a', 'e' change to 'd'). Finally, output the changed file content to the screen.

The question is how do the fwrite and fread work? why can't I get rid off the variable pos2 to simplify the expression. If anyone can help, thanks a lot!

#include <stdio.h>

int main()

{
    FILE *fp;
    char s[20];
    char t[20];
    char transfer;
    int i;
    int pos; // storing the position of the file before reading from the file 
    int pos1; // check the position of the file
    int pos2; // storing the position of the file after reading from the file
#pragma region create a file named "Hello", write "Hello, world" into the file, close it
    if ((fp = fopen("Hello", "wb") )== NULL)
    {
        printf("can't open file\n");
        exit(0);
    }
    strcpy(s, "Hello, world");
    fwrite(s, sizeof(char)*20, 1, fp);
    fseek(fp, 0, SEEK_SET);
    fclose(fp);
#pragma endregion create a file named "Hello", write "Hello, world" into the file, close it
#pragma region read from the file named "Hello", deal with its current, write the change into the file.
    if ((fp = fopen("Hello", "rb+")) == NULL )
    {
            printf("can't open file\n");
            exit(1);
    }
    i = 0;
    while(i < 20) 
    {
            // 提问,该处为何不能利用fwrite的自动定位免去注释掉的语句行(即使用了pos2的语句行)。
            // Here is the problem. since the fread and fwrite function can move the position of the 
            // file, I think I can get rid off the following commented two sentences with the 
            // variable pos2 in it
            pos = ftell(fp);     // storing the position before reading from file
            fread(&transfer, sizeof(char), 1, fp); // the position of the file moved to the next char
            // pos2 = ftell(fp);  // storing the position after reading from file
            pos1 = ftell(fp);
            if (transfer >= 'e')  // if the character greater or equal to 'e' minus 1.
            {
                transfer -= 1;
            }
            fseek(fp, pos, SEEK_SET); // back to the position where the character is read to change the char
            fwrite(&transfer, sizeof(char), 1, fp);// the position of the file moved to the next char
            // fseek(fp, pos2, SEEK_SET); // 
            pos1 = ftell(fp);
            i++;
    }
    fseek(fp, 0, SEEK_SET);
    fclose(fp);
#pragma endregion read from the file named "Hello", deal with its current, write the change into the file.
#pragma region read from the file named "Hello", output the changed string
    if((fp = fopen("Hello", "rb")) == NULL)
    {
            printf("Can't open file\n");
            exit(2);
    }
    fread(t, sizeof(char)*20, 1, fp);
    printf("The output is: %s \n", t); 
// the right output is (the two sentences above with pos2 in it is commented) :
// The output is: Hdkkn,vnqkd
// the wrong output is (the two sentences above with pos2 in it isn't commented): 
// The output is: Hddddddddddddddddddd烫烫烫烫Hello, world
    fseek(fp, 0, SEEK_SET);
    fclose(fp);
#pragma endregion  read from the file named "Hello", output the changed string
    system("pause");
}

解决方案

I didn't actually get the point why you are trying to comment in/out the 2 lines. Because nothing changes whether you comment them in or comment them out. You have already got rid of pos2 in your code (which is what you are asking).

So if you use the following code for your while loop

pos = ftell(fp);     // storing the position before reading from file
fread(&transfer, sizeof(char), 1, fp); 
pos1 = ftell(fp);
if (transfer >= 'e')  // if the character greater or equal to 'e' minus 1.
{
    transfer -= 1;
}
fseek(fp, pos, SEEK_SET);
fwrite(&transfer, sizeof(char), 1, fp);
i++;

then you get "The output is: Hdkkn,vnqkd" which is the expected result.

You could also take each line from the file to an array and make operations on it then write it back to the file. By this way, it could be more generic and you don't have to use magic numbers like "20".

EDIT:

I use gcc 4.5.2 on my linux platform. I don't want to comment on other platforms but as I mentioned before you could take the line to a buffer and then after operation you could write it back. You could try to replace the following code with your while loop:

char line[20] = {0};

fread(line, sizeof(char), 20, fp);

for(i = 0; i < strlen(line); i++)
{
    if(line[i] >= 'e')
        line[i] -= 1;
}

fseek(fp, 0, SEEK_SET);
fwrite(line, sizeof(char), strlen(line), fp);

By this way you could get rid of many variables. It is your choice.

这篇关于同时在一个流中使用的fread和fwrite引起的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆