ignoring \0 within char

06/05/2011 - 08:38 von sebastian | Report spam
Hello,

i try to read out information from a file. there will be sometime
within the read information a charendterminator. is there a possibilty
to ignore this and step to the next character?

gcc options

Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/
README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/
usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-
linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-
included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/
include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --
enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --
disable-
werror --with-arch-32=i686 --with-tune=generic --enable-
checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --
target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

input line

T^@e^@s^@t

code example

long lReadBytes = 0;
size_t t = 100;
char *buffer = (char *) malloc(sizeof(char*));
buffer = NULL;

while ( (lReadBytes = getline( &buffer, &t, pdfstream)) > 0 )
{
int pos = 0;
for (pos = 0; pos <= lReadBytes; pos++)
{
printf("%c", buffer[pos]);
}
}


thanks.
 

Lesen sie die antworten

#1 Markus Wichmann
06/05/2011 - 11:18 | Warnen spam
On 06.05.2011 08:38, sebastian wrote:
Hello,

i try to read out information from a file. there will be sometime
within the read information a charendterminator. is there a possibilty
to ignore this and step to the next character?

gcc options

Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/
README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/
usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-
linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-
included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/
include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --
enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --
disable-
werror --with-arch-32=i686 --with-tune=generic --enable-
checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --
target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

input line

T^@e^@s^@t

code example

long lReadBytes = 0;
size_t t = 100;
char *buffer = (char *) malloc(sizeof(char*));
buffer = NULL;




Oh, come on, you are not for real, are you?

First you allocate via malloc a buffer the size of 1 byte (which means
that the overhead of using malloc is by far greater than the gain), and
then you dismiss it? (Without freeing by the way). Simply drop that
initialization.

while ( (lReadBytes = getline( &buffer, &t, pdfstream)) > 0 )
{
int pos = 0;
for (pos = 0; pos <= lReadBytes; pos++)
{
printf("%c", buffer[pos]);
}
}


thanks.



However, getline() is a POSIX-specific function, and a pretty new one at
that (standardized in 2008). The strictly conforming solution for this
application would be (including headers would be your homework, I'm too
lazy for that now)

#define BUFSIZE 4096
//adjust if needed

int main(int argc, char* argv[])
{
char* filename;
FILE* file;
static char buffer[BUFSIZE];
if (argc < 2) return 1;
filename = argv[1];
file = fopen(filename, "r");
if (!file) return 2;

while (fgets(buffer, sizeof buffer, file))
{
size_t len = strlen(buffer);
for (size_t i = 0; i < len; i++)
{
if (putchar(buf[i]) == EOF) {
fclose(file);
return 3;
}
if (putchar('') == EOF) {
fclose(file);
return 4;
}
}
}
fclose(file);
return 0;
}

HTH,
Markus

Ähnliche fragen