www.digitalmars.com         C & C++   DMDScript  

D - Errors in simple program.

reply madamephreaker hotmail.com writes:
When I use printf in the way i'm doing now(see code) it crashes flat out. 
If i just do "printf(input);" it works until it gets to a blank line.

In all cases it gives "Error: Access Violation" when it crashes.

--- Begin hello.d
import c.stdio;
import stream;

int main( char[][] args )
{
char[] input="";
File myFile = new File(args[1]);
while( ! myFile.eof() )
{
input = myFile.readLine();
printf("%s\n", input);
}
myFile.close();
return 0;
}
--- End hello.d
Mar 20 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
madamephreaker hotmail.com wrote:
 printf("%s\n", input);
Use "%.*s\n".
Mar 20 2003
parent reply madamephreaker hotmail.com writes:
In article <b5dglk$28cu$1 digitaldaemon.com>, Burton Radons
says...
madamephreaker hotmail.com wrote:
 printf("%s\n", input);
Use
"%.*s\n".

What about the multiple lines? and when i use printf(input);
?
input isn't always declared? I'm assuming since result in the stream class is
a resizable array when it doesn't get anything it it it's a pointer to an array
of length zero? Thus accessing it blows up?  Why would "%.*s" fix that?
Mar 20 2003
next sibling parent madamephreaker hotmail.com writes:
In article <b5dm68$2c4e$1 digitaldaemon.com>, madamephreaker hotmail.com
says...
In article <b5dglk$28cu$1 digitaldaemon.com>, Burton
Radons
says...
madamephreaker hotmail.com wrote:
 printf("%s\n",
input);
Use
"%.*s\n".

I just switched to using that now, and it works. to some extent.. If the last line of the file doesn't end in a newline it spews this out: not enough data in stream Although looking at it more i think dli 0.1.2 just has an old version of phobos. (I tried updating but it failed to compile parts of phobos then)
Mar 20 2003
prev sibling parent "Walter" <walter digitalmars.com> writes:
<madamephreaker hotmail.com> wrote in message
news:b5dm68$2c4e$1 digitaldaemon.com...
Why would "%.*s" fix that?
The input[] is a dynamic array, which consists of a length field and a pointer to the data. %s expects a pointer to a null terminated string. %.*s expects two parameters, a length and a pointer, which fits perfectly with D arrays.
Mar 21 2003