从另一个文件 C++ 中的文件名创建文件

发布于 2024-08-24 03:26:17 字数 1374 浏览 15 评论 0原文

我正在用 C++ 对几个大文件进行排序。我有一个文本文件,其中包含所有输入文件的名称,每行一个。我想一次读取一个文件名,将它们存储在一个数组中,然后使用每个名称创建一个文件。现在,我正在使用 fopen 和 fread,它们需要字符数组(我正在尝试优化速度),因此我的文件名被读入字符数组数组。然而,这些数组需要预先固定一个最大大小,因此如果文件名小于最大值,则其余部分将充满垃圾。然后,当我尝试在 fopen() 中使用该数组作为文件名时,它无法识别该文件,因为它在字符串末尾有垃圾。我该如何解决这个问题?这是我的代码:

 #include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
#define NUM_INPUT_FILES 4

using namespace std;



FILE *fp;
unsigned char *buff;
FILE *inputFiles[NUM_INPUT_FILES];


int _tmain(int argc, _TCHAR* argv[])
{


    buff = (unsigned char *) malloc(2048);
    char j[8];
    char outputstring[] = "Feelings are not supposed to be logical. Dangerous is the man who has rationalized his emotions. (David Borenstein)";

    fp = fopen("hello.txt", "r");

    string tempfname[NUM_INPUT_FILES];
    //fp = fopen("hello.txt", "r");
    for(int i=0;i<NUM_INPUT_FILES;i++)
    {
        fgets(tempfname[i], 20, fp);
        cout << tempfname[i];
    }
    fclose(fp);

    for(int i=0; i<NUM_INPUT_FILES;i++)
    {
        fp = fopen(tempfname[i], "w");
        //fwrite(outputstring, sizeof(char), sizeof outputstring/sizeof(char), fp);
        if(fp)
        {
            fclose(fp);}
        else
            cout << "sorry" << endl;
    }


    return 0;
}

另外,如何找到缓冲区的大小以使用 fwrite() 将其写出?

非常感谢, BSG

I am working on sorting several large files in C++. I have a text file containing the names of all the input files, one on each line. I would like to read the file names in one at a time, store them in an array, and then create a file with each of those names. Right now, I am using fopen and fread, which require character arrays (I am trying to optimize for speed), so my filenames are read into an array of character arrays. Those arrays, however, need to have a maximum size fixed in advance, so if the filename is smaller than the maximum, the rest is full of garbage. Then, when I try to use that array as the filename in fopen(), it doesn't recognize the file because it has garbage at the end of the string. How can I solve this problem? Here is my code:

 #include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
#define NUM_INPUT_FILES 4

using namespace std;



FILE *fp;
unsigned char *buff;
FILE *inputFiles[NUM_INPUT_FILES];


int _tmain(int argc, _TCHAR* argv[])
{


    buff = (unsigned char *) malloc(2048);
    char j[8];
    char outputstring[] = "Feelings are not supposed to be logical. Dangerous is the man who has rationalized his emotions. (David Borenstein)";

    fp = fopen("hello.txt", "r");

    string tempfname[NUM_INPUT_FILES];
    //fp = fopen("hello.txt", "r");
    for(int i=0;i<NUM_INPUT_FILES;i++)
    {
        fgets(tempfname[i], 20, fp);
        cout << tempfname[i];
    }
    fclose(fp);

    for(int i=0; i<NUM_INPUT_FILES;i++)
    {
        fp = fopen(tempfname[i], "w");
        //fwrite(outputstring, sizeof(char), sizeof outputstring/sizeof(char), fp);
        if(fp)
        {
            fclose(fp);}
        else
            cout << "sorry" << endl;
    }


    return 0;
}

Also, how do I find the size of a buffer to write it out with fwrite()?

Thank you very much,
bsg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

Spring初心 2024-08-31 03:26:17

正如 Don Knuth 所说,过早的优化是万恶之源。

你的文件名绝对不是瓶颈!只需使用 std::string 即可。

您需要将 fp = fopen(tempfname[i], "w"); 替换为 fp = fopen(tempfname[i].c_str(), "w");< /code> 然而。

As Don Knuth said, premature optimization is the root of all evil.

Your filenames are definitely not the bottleneck! Just use std::string for them.

You'd need to replace fp = fopen(tempfname[i], "w"); with fp = fopen(tempfname[i].c_str(), "w"); however.

迷你仙 2024-08-31 03:26:17

在此阶段忘记优化。
使用 std::vector 并使您的程序正常运行。
一旦它开始工作,如果速度真的那么重要,那么你可以回去改变它

Forget optomizing at this stage.
Use std::vector<std::string> and get your program working.
Once it is working, if speed is really that crucial then you can go back and change it

蝶…霜飞 2024-08-31 03:26:17

如果您使用的是 C 类型习惯用法,那么如果您使用 C++ 进行 google 文件处理,那就更好了。如果您是一名 C 程序员,那么一开始这有点奇怪,但是绝对值得花精力去研究如何以 C++ 方式做事

you are using C type idioms, it would be better if you go google file handling in C++. which is a bit strange to start with if you're a C programmer, but its definitely worth the effort to work out how to do thing the C++ way

櫻之舞 2024-08-31 03:26:17

您需要添加一个空字节并去除新行,因此在第一个 for 循环中编写一个 for 循环来搜索换行符并将其替换为空字节。

尽管其他人是对的,但您在优化尝试中受到了严重误导。

并确保您释放了 malloc 的内容。您应该使用 STL 的另一个充分理由。

You need to add a null byte and strip the new line, so write a for loop within your first for loop that searches for the newline and replaces it with a null byte.

Although the others are right that you are seriously misguided in your optimization attempts.

And make sure you're freeing what you malloc. Another good reason why you should be using the STL.

抱着落日 2024-08-31 03:26:17

如果一次一行读取文件,则可以仅为每一行分配所需的空间量,并以这种方式构建行数组。

我可以理解这对你来说可能不够快,所以作为替代方案。我可以建议

  1. 获取文件的大小
  2. ,分配该大小的缓冲区,
  3. 将整个文件读入缓冲区。
  4. 扫描缓冲区,用 \0 替换 \r 和 \n,并将每行的开头存储在 char* 类型的向量中

If you read the files one line at a time, you can then allocate only the amount of space for each line that is needed and build your array of lines up in that way.

I can understand that this may not be fast enough for you, so as an alternative. may I suggest

  1. get the size of the file
  2. allocate a buffer of that size
  3. read the entire file into the buffer.
  4. scan the buffer replacing \r and \n with \0 and storing the start of each line in a vector of type char*
尘曦 2024-08-31 03:26:17

我和这里的其他人一样,这是不成熟的优化。

我不明白 fgets(tempfname[i], 20, fp); 如何编译,更不用说工作了,因为 tempfname[i] 是一个 string& ;fgets 需要 char*

也许你想要

typedef char file_name[20]; // way too short
file_name tempfnames[NUM_INPUT_FILES];

尽管,除了我在这里所做的许多其他更改之外,你可以完全在每次循环迭代上处理一个文件,并完全避免拥有一个名称数组。

I'm with everyone else here, this is premature optimization.

I don't see how fgets(tempfname[i], 20, fp); could compile, much less work, since tempfname[i] is a string& and fgets requires a char*.

Probably you want

typedef char file_name[20]; // way too short
file_name tempfnames[NUM_INPUT_FILES];

Although, among many other changes I would make here, you could entirely handle a file on each loop iteration and avoid having an array of names entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文