Strange error with getline 
Author Message
 Strange error with getline

Hi Gurus,

Have a look at the following code:

awk '{ if ($1 ~ "#include")
       {
         n = split ($2, arr, "\"|<|>");

         fileName = arr[2];

         print fileName;

         while((getline line < fileName) == 1) { print line; }

       }
       else
       {
         print $0;
       }

Quote:
}'

And before you tell me, i know that getline is NOT supposed to
be (mis)used in this way :-). This is a trainee's first attempt.

I'm trying to scan a C/C++ source file looking for #include's
and replacing them with the contents of the file. I get this error:

awk: cmd. line:8: (FILENAME=anand.cpp FNR=9) fatal: expression for `<'
                   redirection has null string value

When i print the variable fileName, I get a proper string of the form
x.h. Why is this happening? Is it just me or ...?

Thanks in advance,

Best Regards
Sai Shankar



Sun, 17 Aug 2003 15:26:33 GMT  
 Strange error with getline

Quote:

> Hi Gurus,

> Have a look at the following code:

> awk '{ if ($1 ~ "#include")
>        {
> n = split ($2, arr, "\"|<|>");

>          fileName = arr[2];

> print fileName;

>          while((getline line < fileName) == 1) { print line; }

>        }
>        else
>        {
>          print $0;
>        }
> }'

> And before you tell me, i know that getline is NOT supposed to
> be (mis)used in this way :-). This is a trainee's first attempt.

> I'm trying to scan a C/C++ source file looking for #include's
> and replacing them with the contents of the file. I get this error:

> awk: cmd. line:8: (FILENAME=anand.cpp FNR=9) fatal: expression for `<'
>                    redirection has null string value

> When i print the variable fileName, I get a proper string of the form
> x.h. Why is this happening? Is it just me or ...?

> Thanks in advance,

> Best Regards
> Sai Shankar

Try putting this in getlin1.awk:

    $1 ~ "#include" {
      n = split ($2, arr, "\"|<|>")
      fileName = arr[2]
      print fileName
      while((getline line < fileName) == 1) {
        print line
      }
      close fileName
      next
    }
    { print }

then run as:

$ awk -f getlin1.awk test.c

works OK - provided that the include files are in the current directory.  It
will fail for those that are in system include directories unless you make
some changes.  In fact any header in "<>" should refer to system includes
anyway so you probably need only check for quoted ones.

HTH
Peter
--
Peter S Tillier

Usual disclaimers, E&OE



Sun, 17 Aug 2003 19:54:22 GMT  
 Strange error with getline

...
Quote:
>I'm trying to scan a C/C++ source file looking for #include's
>and replacing them with the contents of the file. I get this error:

...

While it's an interesting awk exercise, it's wastefully redundant. All C/C++
compilers include an option to just preprocess the source files. GCC
includes additional options to control what gets preprocessed and what comes
through as output.

But as for the awk exercise, I'd do it this way.

#preprocinc.awk
BEGIN { FS = "[\"<> \t]+" }
/^[ \t]*#include[ \t]+/ {
  fn = $(2 + ($1 == ""))
  printf("// start of %s //\n", fn)
  while ((getline s < fn) > 0) print s
  close(fn)
  printf("//  end of %s  //\n", fn)
  next

Quote:
}

{ print }

Then run the command

awk -f preprocinc.awk sourcefile.c

I'd bet your problem is shell-induced. What OS and what awk implementation
are you using?



Mon, 18 Aug 2003 02:33:50 GMT  
 Strange error with getline

Quote:



> ...
> >I'm trying to scan a C/C++ source file looking for #include's
> >and replacing them with the contents of the file. I get this error:

[snip]

Quote:

> I'd bet your problem is shell-induced. What OS and what awk implementation
> are you using?

Gawk with Cygwin (2.0) with bash2.02 on NT (SP5)

The idea is the same (use getline), however it fails. The workaround we
found is to use:

$2 = arr[2] in the original code. Assigning arr[2] to a variable and using
that with getline doesn't work. Material for a bug report?

Best Regards
Sai Shankar



Mon, 18 Aug 2003 11:35:53 GMT  
 Strange error with getline

...

Quote:
>Gawk with Cygwin (2.0) with bash2.02 on NT (SP5)

>The idea is the same (use getline), however it fails. The workaround we
>found is to use:

>$2 = arr[2] in the original code. Assigning arr[2] to a variable and using
>that with getline doesn't work. Material for a bug report?

Before sending a bug report, I'd strongly urge you to replace the statements

n = split ($2, arr, "\"|<|>");
:
print fileName;

with

n = split ($2, arr, "[\"<>]");
:
printf("[%s]\n", fileName);

and run the modified script. The first change is just for efficiency:
/"|<|>/ is poor usage compared to /["<>]/ (functionally equivalent, yes, but
so are x++ and x = x + int(sin(3.14159 / 4) + .5)). The second would show if
any stray whitespace characters were in fileName (how that could be I can't
say), which would explain why $2 = arr[2] rather than fileName = arr[2]
would have fixed things.



Tue, 19 Aug 2003 02:37:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Get Strange error error.bmp [01/26]

2. strange error link errors when using tclpro1.41 and VC++ 5.0

3. Presenter strange "error"

4. Strange Error

5. Strange link error every 2. time

6. Strange error when updating a record through ODBC

7. Strange compiler error in C4

8. Strange "file not open" error

9. File Error 1735 - strange behaviour

10. Strange error URGENT!!!

11. Strange error with ODBC

12. Strange C4a (ABC) error in Form update

 

 
Powered by phpBB® Forum Software