标签: file-processing

如何正确解析我的文件?(使用break/continue)

我有以下数据,例如:

34 foo
34 bar
34 qux
62 foo1
62 qux
78 qux

这些是基于第一列排序的.

我想要做的是处理以34开头的行,但我还希望文件迭代在找不到34s之后退出,而不必扫描整个文件.我该怎么做?

原因是因为要处理的行数非常大(~10 ^ 7).那些以34开头的人只占其中的1-10%左右.

我知道我可以grep这些行并将其输出到另一个文件中,但这太繁琐了,并且会产生更多的磁盘空间消耗.

此代码说明了使用"continue"失败的尝试:

#include <iostream>
#include <vector>
#include <fstream>       
#include <sstream>       
using namespace std;     

int main () {
    string line;
    ifstream myfile ("mydata.txt");
    vector<vector<string> > dataTable;
    if (myfile.is_open())
    {
        while (! myfile.eof() )   
        {
                stringstream ss(line);    
                int FirstCol;
                string SecondCol;

                if (FirstCol != 34) {
                   continue;
                }

                // This will skip those other than 34
                // but will still iterate through all the …
Run Code Online (Sandbox Code Playgroud)

c++ iteration file-processing

1
推荐指数
1
解决办法
277
查看次数

从文件中读取C,它显示为"@"

我正在尝试用C语言读取文件.但是当我读到并将其写入stdout时,它也会打印@在我的文件中没有.是什么原因?

    #include <stdio.h>

int main() {

FILE *fp;
int br;
char buffer[10];
int i;
fp = fopen("a.txt","r");
while(1) {
        br = fread(buffer,1,10,fp);
        printf("%s",buffer);
        if (br==0)
                break;
        }
}
Run Code Online (Sandbox Code Playgroud)

输出:

1234567891 @ 2345678912 @ 3456789 12 @ 3456789 12 @

文件:123456789123456789123456789

c file file-processing

1
推荐指数
1
解决办法
115
查看次数

文件处理 - Haskell

我如何在haskell中实现以下内容:

  1. 我从命令行收到一个输入文件.此输入文件包含用制表符,换行符和空格分隔的单词.
  2. 我必须用逗号替换这些元素(制表符,换行符和空格).
  3. 然后将结果写入一个名为的文件output.txt.

任何帮助深表感谢.我的哈克尔技能仍在发展中.


到目前为止,我有这个代码:

    processFile::String->String
    processFile [] =[]
    processFile input =input

    process :: String -> IO String
    process fileName = do
    text <- readFile fileName
    return (processFile text)

    main :: IO ()
    main = do
    n <- process "input.txt"
    print n
Run Code Online (Sandbox Code Playgroud)

在processFile函数中,我应该处理输入文件中的文本.

haskell file-processing winghci

1
推荐指数
1
解决办法
517
查看次数

是否可以在不使用内置.reverse()函数的情况下反转列表中项目的顺序?

我必须编写一个程序来反转文件中单词的字母.

例如,如果文件包含以下单词:

snow   
tree
star
wreath
Run Code Online (Sandbox Code Playgroud)

它会将它们变成:

wons
eert
rats
htaerw
Run Code Online (Sandbox Code Playgroud)

完成后,我必须编写一个新文件,它将以相反的顺序写入它们,如下所示:

htaerw
rats
eert
wons
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

def reverse(string):
   #empty list for string
   word = []

   #for each letter in the string obtain the corresponding opposite number
   #first letter to last letter, second letter to second last letter, etc...

   for letter in range(len(string)-1, -1, -1):
       word.append(string[letter])

#create main() function
def main():
    #open file and read words in each line
    input_file = open("words.txt", "r")
    word_file = input_file.readlines()
    #empty list for the …
Run Code Online (Sandbox Code Playgroud)

python function file-processing

-2
推荐指数
1
解决办法
215
查看次数

标签 统计

file-processing ×4

c ×1

c++ ×1

file ×1

function ×1

haskell ×1

iteration ×1

python ×1

winghci ×1