Hi,
when i run this program, the program hangs when i enter input value as CTRL +Z.
Also i am getting a segmentation fault when i debug this program.
also please check the for loop at the end after the while. strange thing is that for all values of i, im getting the same char* value as the last one i entered.
Please check out the below code..
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int getline(char[],int);
int readlines(char*[], int);
//char *malloc(int);
void sorts(char** entry,int);
main()
{
char* entry[1000];
char* a;
int i = 0,nlines;
char s[10];
P points to an array of chars.i am copying the array of chars(lines) to p because lineptr is an array of pointers to char. Is something wrong here?
because the program hangs only when i enter ctrl+Z to terminate the input(EOF).
Where are you allocating any arrays? I see alot of pointers but no allocations (new or malloc). But then again it's hard to read unformatted code, use [code ][/code ] tags.
in the getline function,lines is an array or chars whos length is returned by getline which is called in readlines function. since there is a while loop with a condition that len .....> 0, when i enter CTRL +Z, len = 0, hence it does not enter the while loop. Anything wrong here ? This is one of the example programs in Retchie's C book. It worked fine using borland c compiler though.
yeah i think this is where i missed to malloc..
......
while((len = getline(lines,maxlen)) > 0 )
if(nlines >= maxlines)..
it should have been if(nlines >= maxlines||((p = (char*) malloc(len)) = NULL)
.......
......
lemme try this.
anyways where are you people located? cire and naraku9333 ?
getline is capable of a buffer overrun (the for condition should be i < lim-2.
getline returns the length of the c-string found, and not the actual size so your malloc(len) would be allocating an incorrect size. Should be malloc(len+1), but why are you using malloc in C++?
im in the process of moving from c to c++. so i guess in many places, i have written for c way. for..
1 int maxlen = 1000;
2 char lines[maxlen];
i tried using a macro for maxlen. but got some error. i will change this. and also i think u are referring to the new operator in c++ instead of malloc.
but the point is dev c++ compiler comes with a stdio.h which does support C like commands. i have also included stdio.h in my code.
Do u think this is totally illegal ?
great thanx!!
i dont like dev c++ anyways. i tried running turbo c++ 3.0, since i have windows 7 64 bit machine, i also tried mounting the TC directory on the MS DOS X shell and running turbo c++. but i am facing some issues doing that.
i did download visual c++ express 10.0 which was an evaluation version.
also i think the best is to try eclipse and use a maven project (pom.xml) to build the project.
which one do u recommend ?
The evaluation version, perhaps more commonly known as the free version, of VC++10 is fully functional, you just don't get the extras you're not paying for.
eclipse is an ide.
TC++ 3.0... You wanted to use a compiler that hasn't been updated since 1991?
For Windows programming, I think the popular free compiler choices boil down to a variant of GCC (either mingw or cygwin), clang, or VC++. There used to be Open Watcom... I don't know if that is still being developed. I haven't tried out clang yet.
The VS IDE is top-notch and the biggest reason I prefer using visual studio.