howto:读取输入并将其存储在另一个文件中

时间:2015-05-14 17:20:27

标签: c++

我想创建一个程序,从一个文件读取最高值并将其存储在另一个文件中。我已经读过ifstream和ofstream,但是如何让ofstream在另一个文件中存储最大值?以下是我到目前为止的情况:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

struct CsvWhitespace : ctype<char> {
static const mask* make_table() {
    static vector<mask> v{classic_table(), classic_table() + table_size};
    v[','] |=  space;  // comma will be classified as whitespace
    return v.data();
}
CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
}    csvWhitespace;

int main() {
string line;
ifstream myfile ("C:/Users/Username/Desktop/log.csv");


ofstream myfile2 ("C:/Users/Username/Desktop/log2.csv");


return 0;
}
auto v = vector<int>{};
myfile.imbue(locale{myfile.getloc(), &csvWhitespace});
copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
myfile2 << *max_element(begin(v), end(v));
}

提前致谢:)

3 个答案:

答案 0 :(得分:0)

您可以通过在二进制模式下处理它们来复制另一个文件,而不必担心格式。这是一个例子:

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

#define bufSize 1024

int main(int argc, char *argv[])
{
  FILE *ifp, *ofp;
  char buf[bufSize];
  if (argc != 3)
  {
    fprintf(stderr,
            "Usage: %s <soure-file> <target-file>\n", argv[0]);
    return 1;
  }
  if ((ifp = fopen(argv[1], "rb")) == NULL)
  { /* Open source file. */
    perror("fopen source-file");
    return 1;
  }
  if ((ofp = fopen(argv[2], "wb")) == NULL)
  { /* Open target file. */
    perror("fopen target-file");
    return 1;
  }
  while (fgets(buf, sizeof(buf), ifp) != NULL)
  { /* While we don't reach the end of source. */
    /* Read characters from source file to fill buffer. */
    /* Write characters read to target file. */
    fwrite(buf, sizeof(char), strlen(buf), ofp);
  }
  fclose(ifp);
  fclose(ofp);
  return 0;
}

在IP中作为示例提供,source。您只需将cmd参数指定为所需文件即可。

答案 1 :(得分:0)

你可以这样做。 Live example使用cincout而不是文件。

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

struct CsvWhitespace : ctype<char> {
    static const mask* make_table() {
        static vector<mask> v{classic_table(), classic_table() + table_size};
        v[','] |= space;  // comma will be classified as whitespace
        return v.data();
    }
    CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
};

int main() {
    string line;
    ifstream myfile("log.csv");
    ofstream myfile2("log2.csv");
    auto v = vector<int>{};
    myfile.imbue(locale{myfile.getloc(), new CsvWhitespace{}});
    copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
    myfile2 << *max_element(begin(v), end(v));
}

答案 2 :(得分:0)

直到没有更多的代币

  1. 从输入文件中读取令牌
  2. 转换为整数以确保它是有效整数
  3. 既然它是一个整数,也可以采用简单的路线并将其与目前为止看到的最大数字进行比较。保留数字,如果它更大
  4. 有趣。格式化中断并且没有正确呈现代码而没有一些文本。

    long largest = LONG_MIN;
    std::string token;
    while (std::getline(myfile, token, ',')) //grab up to next  ',' or end of file
    {
        char * endp; // will be set by strtol to the first character in token 
                     // that isn't a digit
        if (token.length() > 0)
        {
            long number = strtol(token.c_str(), &endp, 10);
            if (*endp == '\0')
            {    // if strtol parsed to the end of the token, the number should be good.
                if (number > largest)
                {  // update largest number if necessary
                    largest = number;
                }
            }
            else
            {   
                // handle bad token
            }
        }
        else
        {   
            // handle bad token
        }
    }
    

    largest应包含最大数字。

    OP似乎已经抓住了对输出文件的写入。

    注意:需要包含登录才能获得LONG_MIN。