从文本文件中复制列

时间:2012-12-20 11:22:33

标签: c

我有一个文本文件,如下所示:

Flooding refers to all water that overflows a node, whether it ponds or not.
  --------------------------------------------------------------------------
                                                             Total   Maximum
                                 Maximum   Time of Max       Flood    Ponded
                        Hours       Rate    Occurrence      Volume     Depth
  Node                 Flooded       CMS   days hr:min    10^6 ltr    Meters
  --------------------------------------------------------------------------
   1064                  0.15     0.000      0  00 00       0.000      0.35
   1065                  0.25     0.078      0  00 09       0.049      0.41
   1130                  0.25     0.626      0  00 00       0.106      0.90
   1155                  0.24     0.098      0  00 07       0.073      0.61
   1173                  0.25     0.106      0  00 15       0.022      0.76

我想复制数字列(无文本),以便生成的文件如下:

   1064                  0.15     0.000      0  00 00       0.000      0.35
   1065                  0.25     0.078      0  00 09       0.049      0.41
   1130                  0.25     0.626      0  00 00       0.106      0.90
   1155                  0.24     0.098      0  00 07       0.073      0.61
   1173                  0.25     0.106      0  00 15       0.022      0.76

直到现在我已经设法在C中执行此代码:

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

void main()
{
FILE *fs,*ft;
int ch;
int c;
fs=fopen("node.txt","r");
if (fs=NULL)
{
    puts("cannot open source file");
    exit(0);
}
ft=fopen("new_node.txt","w");
do
{
   ch=fgetc(fs);
   if (ch=EOF)
   break;
   else
   {
    if (ch>0)
    fputc(ch,ft);
   }
     ch++;
}
  while(1);
  fclose(fs);
  fclose(ft);
  return 0;
}

问题是没有任何东西出来。任何人都可以在这方面提供帮助,并提供有效的代码。

3 个答案:

答案 0 :(得分:1)

if (fs=NULL)NULL分配给fs而非比较它们。使用==来测试相等性,即if (fs==NULL)

这同样适用于您的代码中的if (ch=EOF)

请注意,启用警告的编译可能会在条件表达式中指出此赋值。

这些更改将允许您复制源文件的全部内容。如果您只想复制以空格/数字开头的行,请查看isspaceisdigit。放松使用fgets的建议会使这更加简单。

答案 1 :(得分:1)

您的数据显然是面向行的;它使没有有意义地一次一个字符地读/写这个数据。

使用fgets()将整行读入适当大小的缓冲区(可能为1024字节)。检查线,确定是否要保留。如果是这样,请使用fputs()写下来。

答案 2 :(得分:0)

实际上,您的问题非常简单:您只是不使用正确的工具。实际上,在bash中(默认安装在大多数Linux发行版和Mac OS X上。对于Windows,安装cygwinmingw),这就完成了这项工作:

tail -n +8 copying_columns.data

实际上,这开始在第8行打印文件,有效地跳过标题。 Awk提供了更通用的解决方案:

awk 'x >= 2 {print}; /---/ {++x}' copying_columns.data

即,如果我们遇到包含---的两行或更多行,则打印该行。在perl中,这可以给出:

perl -ne 'if ($x >= 2) {print $_;} if (/---/){++$x;}

或者,如果我们将perl脚本放入其自己的文件copying_columns.perl

#!/usr/bin/perl
use strict;
use warnings;
my $x = 0;
while (<>){
    if ($x >= 2){
        print $_;
    } 
    if (/---/){
        ++$x;
    }
}

使用

运行
 perl copying_columns.perl copying_columns.data

作为奖励,这是一个Python版本:

#!/usr/bin/python
import sys
for f in [open(fname, 'rb') for fname in sys.argv[1:]] or [sys.stdin]:
    x = 0
    for line in f:
        if x >= 2:
            print(line.rstrip('\r\n'))
        if line.find('---') >= 0:
            x += 1
    f.close()

使用

运行
python copying_columns.py copying_columns.data

此版本具有在输入文件之间重置计数器的额外优势。

最后,这是我的两分钱:学习(至少:))其中一个工具:它们非常有效,特别是data manipulation

相关问题