I was actually trying some C programming code and I came up with this program, which for some reason is working only when an integer is declared and is given some value.
The code below works perfectly
#include<stdio.h>
int main()
{
FILE *file = fopen("/proc/cpuinfo","r");
char *line;
int count = 0;
if(file!=NULL)
{
while(fscanf(file," %[^\n]",line)!=-1)
{
printf("\n%s",line);
}
printf("\n\n\t* * * * * * * * * * * * * * * * * * * * * \n\n");
}
else
{
printf("\nFile Does not Exist\n");
}
return 0;
}
but this does not work , (I mean, when I run this I get an infinite loop of (null) values.
#include<stdio.h>
int main()
{
FILE *file = fopen("/proc/cpuinfo","r");
char *line;
if(file!=NULL)
{
while(fscanf(file," %[^\n]",line)!=-1)
{
printf("\n%s",line);
}
printf("\n\n\t* * * * * * * * * * * * * * * * * * * * * \n\n");
}
else
{
printf("\nFile Does not Exist\n");
}
return 0;
}
I am using gcc compiler
gcc -v
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --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.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Can anyone explain what is happening here, I would like to know why this is happening.