阅读和C(一倍)的文件中写 [英] Read and Write within a file in C (double it)

查看:114
本文介绍了阅读和C(一倍)的文件中写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取文件,它多少字节包含读取,然后围绕它其最接近的GB,然后双击该文件的大小。但是,是有办法读取该文件,然后有的做这一切的东西放回同一个文件?

下面是我到目前为止,但它会创建新内容的新文件,但我不知道如果我的逻辑是正确的。

另外,你创建一个常数像的#define BYTE?

到目前为止,作为一个测试情况下,我只是用字节作为一个int,使其等于50

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;//的#define BYTE 50诠释的main()
{
    FILE * FP1,FP2 *;
    INT CH1;
    clock_t表示已过;
    焦炭fname1 [40],fname2 [40];
    所以char a;    的printf(文件输入名称:);
    与fgets(fname1,40,标准输入);
    而(fname1 [strlen的(fname1) - 1] =='\\ n')
    {
        fname1 [strlen的(fname1)-1] ='\\ 0';
    }    FP1 = FOPEN(fname1,R);
    如果(FP1 == NULL)
    {
        的printf(不能读\\ n打开%s,fname1);
        出口(1);
    }    的printf(这个程序会总结当前文件到最高的GB,然后双击它);    经过=时钟(); //获取启动时间    CH1 = GETC(FP1); //读取从每个文件中的值    INT NUM = 50;    INT字节= 0;    而(1)//继续阅读,而值相等或不相等;如果达到其中的一个文件月底止结束
    {
        CH1 = GETC(FP1);        字节++;        如果(CH1 == EOF)//如果任一文件已到达末尾,那么它的结束了!
        {
            打破; //如果任一值是EOF
        }
    }    在GB // 1,000,000,000字节
    INT nextInt =字节%NUM;    //例如:2.0GB 20亿 - 13亿1.3GB = 70亿OR同样的事情2,000,000,000%13亿= 7亿    INT计数器= 0;    的printf(请输入文件名,你想创建:);
    与fgets(fname2,40,标准输入);
    而(fname2 [strlen的(fname2) - 1] =='\\ n')
    {
        fname2 [strlen的(fname2)-1] ='\\ 0';
    }    FP2 = FOPEN(fname2,W);
    如果(FP1 == NULL)
    {
        的printf(不能读\\ n打开%s,fname2);
        出口(1);
    }    如果(FP2 == NULL)
    {
     看跌期权(无法打开此文件);
     FCLOSE(FP1);
     出口(1);
    }    而(计数器!= nextInt)
    {
     A =龟etc(FP1);
     的fputc(一,FP2);
     反++;
    }    FCLOSE(FP1); //关闭文件
    FCLOSE(FP2);    的printf(在文件%U的总字节数:字节);
    的printf(圆了下GB%D:,nextInt);    经过=时钟() - 经过; //经过时间
    的printf(这花了%.4f秒\\ n,(浮点)已过/ CLOCKS_PER_SEC);
    返回0;
}


解决方案

您正在工作方式太辛苦。我会假设你的操作系统是Windows或Linux。

在Windows上, _stat 会得到一个文件的确切长度。在Linux中它是统计。双方将做到这一点从文件系统的信息,所以它几乎是瞬间的。

在Windows上, _chsize 将文件扩展到任何字节数。在的Linux ftruncate 。操作系统将零写入的延伸,所以这将是一个快速写入确实如此。

在所有的情况下,简单通过搜索找到的文档。

在code将成为直线(无环路),约10行。

四舍五入到下一个GB是简单地用

完成

 的#define GIGA((为size_t)1 LT;< 30)
为size_t new_size =(old_size + GIGA - 1)及〜(GIGA - 1);

I am trying to read a file, read in how many bytes it contains and then round it up to its nearest GB and then double the file size. However, is there is way to read the file and then some do all this stuff back into the same file?

Here is what I have so far, but it creates a new file with the new contents but I'm not sure if my logic is correct

Also, do you create a constant like BYTE with #define?

So far as a test case I just used byte as an int and make it equal to 50

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

// #define BYTE 50

int main()
{
    FILE *fp1, *fp2;
    int ch1;
    clock_t elapsed;
    char fname1[40], fname2[40];
    char a;

    printf("Enter name of the file:");
    fgets(fname1, 40, stdin);
    while ( fname1[strlen(fname1) - 1] == '\n')
    {
        fname1[strlen(fname1) -1] = '\0';
    }

    fp1 = fopen(fname1, "r");
    if ( fp1 == NULL )
    {
        printf("Cannot open %s for reading\n", fname1 );
        exit(1);
    }

    printf("This program will round up the current file into highest GB, and then double it");

    elapsed = clock(); // get starting time

    ch1  =  getc(fp1); // read a value from each file

    int num = 50;

    int bytes = 0;

    while(1) // keep reading while values are equal or not equal; only end if it reaches the end of one of the files
    {
        ch1 = getc(fp1);

        bytes++;

        if (ch1 == EOF) // if either file reaches the end, then its over!
        {
            break; // if either value is EOF
        }
    }

    // 1,000,000,000 bytes in a GB 
    int nextInt = bytes%num;

    // example: 2.0GB 2,000,000,000 - 1.3GB 1,300,000,000 = 7,000,000,000 OR same thing as 2,000,000,000%1,300,000,000 = 700,000,000

    int counter = 0;

    printf("Enter name of the file you would like to create:");
    fgets(fname2, 40, stdin);
    while ( fname2[strlen(fname2) - 1] == '\n')
    {
        fname2[strlen(fname2) -1] = '\0';
    }

    fp2 = fopen(fname2, "w");
    if ( fp1 == NULL )
    {
        printf("Cannot open %s for reading\n", fname2);
        exit(1);
    }

    if(fp2 == NULL)
    {
     puts("Not able to open this file");
     fclose(fp1);
     exit(1);
    }

    while(counter != nextInt)
    {
     a = fgetc(fp1);
     fputc(a, fp2);
     counter++;
    }

    fclose(fp1); // close files
    fclose(fp2);

    printf("Total number of bytes in the file %u: ", bytes);
    printf("Round up the next GB %d: ", nextInt);

    elapsed = clock() - elapsed; // elapsed time
    printf("That took %.4f seconds\n", (float)elapsed/CLOCKS_PER_SEC);
    return 0;
}

解决方案

You're working way too hard. I'll assume your OS is Windows or Linux.

On Windows, _stat will get the exact length of a file. In Linux it's stat. Both will do this from file system information, so it's almost instantaneous.

On Windows, _chsize will extend the file to any number of bytes. On Linux it's ftruncate. The OS will be writing zeros to the extension, so it will be a fast write indeed.

In all cases it's simple to find the documentation by searching.

The code will be straight-line (no loops), about 10 lines.

Rounding up to the next GB is simply done with

#define GIGA ((size_t)1 << 30)
size_t new_size = (old_size + GIGA - 1) & ~(GIGA - 1);

这篇关于阅读和C(一倍)的文件中写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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