#include<iostream> #include<cstdio> #include<cstring> #include<alloc.h> #include<cstdlib> using namespace std;
class GraphClass { int arr[1000][1000]; int r,c; public: GraphClass() { for(int i=0;i<1000;i++) memset(*(arr+i),0,sizeof(*(arr+1))); r=c=0; } void ReadFromFile(char *str) { FILE *f; char buff[1000]={'\0'}; f=fopen(str,"r"); char *p; while(!feof(f)) { fgets(buff,sizeof(buff),f); buff[strlen(buff)-1]='\0'; printf("%s\n",buff); int d;c=0; while((p=strchr(buff,32))!=NULL) { sscanf(buff,"%d",&d); // printf("%d ",d); arr[r][c++]=d; strcpy(buff,p+1); } sscanf(buff,"%d",&d); // printf("%d ",d); arr[r][c++]=d; // puts(""); r++; } fclose(f); return; } void displayGraph() { printf("%d %d \n",r,c); /* for(int i=0;i<r;i++){ for(int j=0;j<c;j++) printf("%d ",arr[i][j]); printf("\n"); } */ return ; } }; int main(int argc, char *argv[]) { GraphClass graph; char str[10000]; if(argc < 2) { puts("ERROR cann't continue"); exit (1);} strcpy(str,argv[1]); graph.ReadFromFile(str); graph.displayGraph(); return 0; } Suppose for a file graph.txt bash-3.00$ cat graph.txt 0 2 9999 3 5 2 0 1 4 9999 9999 1 0 9999 2 3 4 9999 0 1 5 9999 9999 1 0 bash-3.00$ g++ -o s Graph.cpp ld: warning: symbol `clog' has differing types: (file /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/libstdc+ +.so type=OBJT; file /usr/lib/libm.so type=FUNC); /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/libstdc++.so definition taken bash-3.00$ ./s graph.txt 0 2 9999 3 2 0 1 4 9999 1 0 9999 3 4 9999 0 5 9999 9999 1 6 1 Now when I execute the file after compilation it give an extra column count ... It is trying to read when the file has reached the EOF .. Any specific reason why it is so ??? I have to check for the termination of the file content by : while(!feof(f)) { fgets(buff,sizeof(buff),f); buff[strlen(buff)-1]='\0'; if(!strcmp(buff,"")) return; ---------->>>>>> Have to Add it to prevent the above problm int d;c=0; while((p=strchr(buff,32))!=NULL) {.............. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.