1.11M Members

Copying numerical columns from a text

 
0
 

I have a text file which looks like this :

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

i want to copy the numerical columns (no text) such that the resulting file is like as such :

   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

Till now i have managed to do this code in 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;
}

The problem is that nothing is coming out of it. Can anyone help in this regard and provide a working code.

 
0
 

You're focusing too much on the little things and not seeing the big picture. The problem is very simple: display any line where the first non-whitespace character is a digit. To do that you read each line in turn, find the first non-whitespace character, and print it if it's a digit. Easy peasy:

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

#define WHITESPACE " \t\n\r\f\v"

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in) {
        char line[BUFSIZ];

        while (fgets(line, sizeof line, in)) {
            char *first = line + strspn(line, WHITESPACE);

            if (isdigit(*first))
                fputs(line, stdout);
        }

        fclose(in);
    }

    return 0;
}
 
0
 

isn't there supposed to be another file on which the new content is to written ?

 
0
 

isn't there supposed to be another file on which the new content is to written ?

I don't know, is there? My code was an example. If you want to modify it or use it as a template for a more specific solution to your problem, then do so. I actually have plenty of my own work to do, and I'm disinclined to add your work to that pile.

 
0
 

thanks very much deceptikon. But how do i get the resulting solution to a new file ?

 
0
 

But how do i get the resulting solution to a new file ?

Open a second FILE* that writes to a file and use it in place of stdout. Do you know about file I/O in C?

 
0
 

ok. Thanks Deceptikon. Ya i Know I/O a little bit. Actually i am very new to programming and thus finding this a bit difficult. But again, thanks a ton for your help.

 
0
 

Actually i am very new to programming and thus finding this a bit difficult.

All the more reason to practice. Though if you have any specific questions I'm happy to assist.

p.s. It doesn't get any easier, you just replace simple problems with more complex problems. ;)

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article