alex_cismas Posted January 25, 2012 Report Posted January 25, 2012 (edited) As vrea si eu sa stiu unde e eroarea in acest program, si de ce se blocheaza in terminal.#include <stdio.h>#include <stdlib.h>#define LINE 100/*void clear_s(char*);*/int main(int argc, char* argv[]){ char *s; int i; int c; char car; if (argc>2) { fprintf(stderr,"EROARE!"); return 1; } FILE *fid; fid=fopen(argv[1],"r"); if (fid==NULL) { fprintf(stderr,"Nu pot sa deschid fisierul!"); return 1; } printf("\n %ld",ftell(fid)); while (!feof(fid)) { i=0; while(1) { fscanf(fid,"%c",&car); if (car=='\n') { break; } i=i+1; } s=(char*)malloc(i*sizeof(char)); fseek(fid,-i,SEEK_CUR); fgets(s,i,fid); c=ftell(fid); printf("%d ::: %s",c,s); free(s); getchar(); } printf("\n"); fclose(fid); return 0;}/*void clear_s(char *s){ int i; for (i=0;i<LINE;i++) { s[i]=0; }}*/ Edited January 25, 2012 by alex_cismas am inlocuit quote cu code Quote
MARIUSCS Posted January 25, 2012 Report Posted January 25, 2012 Ai avut 18 erori...au mai ramas 2 pe care nu le gasesc#include <stdio.h>#include <stdlib.h>#define LINE 100/*void clear_s(char*);*/int main(int argc, char* argv[]);{char *s;int i;int c; char car;if (argc>2);|{fprintf(stderr,"EROARE!");return 1;}FILE *fid;fid=fopen(argv[1],"r");if (fid==NULL){fprintf(stderr,"Nu pot sa deschid fisierul!");return 1; } printf("\n %ld",ftell(fid));while (!feof(fid));{i=0; while(1);{fscanf(fid,"%c",&car);if (car=='\n');{break;}i=i+1; }s=(char*)malloc(i*sizeof(char)); fseek(fid,-i,SEEK_CUR); fgets(s,i,fid);c=ftell(fid);printf("%d ::: %s",c,s);free(s);getchar();}printf("\n");fclose(fid);return 0;}/*void clear_s(char *s){int i;for (i=0;i<LINE;i++){s[i]=0;}}*/ Quote
alex_cismas Posted January 25, 2012 Author Report Posted January 25, 2012 Cum naiba 2 erori, ca mie in terminal cand compilam nu`mi gasea eroare... , Doar mi se bloca in interiorul programului, de parca nu era utilizat brake`ul. Quote
nedo Posted January 25, 2012 Report Posted January 25, 2012 Ce compilator folosesti?Posteaza te rog ce compilator/ide folosesti. Probabil de aia e diferenta in numarul de erori. Quote
MARIUSCS Posted January 25, 2012 Report Posted January 25, 2012 Eu folosesc Turbo C++ 3.2...Fi atent ca e eroare in prima linie la stdio.h si la a 9-a linie cu acolada...zice ca declaratia e terminata incompelt...is clasa 9-a,abia acum invat C++,imi pare rau ca nu te pot ajuta. Quote
em Posted January 25, 2012 Report Posted January 25, 2012 Programul functioneaza conform codului scris de tine. Nu în?eleg problema. Detaliaz?. Quote
MARIUSCS Posted January 25, 2012 Report Posted January 25, 2012 Am completat eu cu niste ";" pe care le-a uitat...dar la restu' nu ma descurc,sunt curios care e problema. Quote
alex_cismas Posted January 25, 2012 Author Report Posted January 25, 2012 In linie de comanda avem gcc -o cat cat_t.c asha e salvat la mine programul,dupa care executam ./cat fisier (putem lua acelasi fisier cat). Acesta va afisa la apasarea butonului enter cate o linie pe ecran, dar nu va mai iesi din program.Dupa afisarea intregului program va afisa niste linii goale. Eroare de care nu`mi dau seama este cum sa fac sa se opreasca dupa terminarea afisarii programului. Quote
alex_cismas Posted January 25, 2012 Author Report Posted January 25, 2012 nu prea ma pricep .. la debug Quote
alien Posted January 25, 2012 Report Posted January 25, 2012 pai zici ca-ti da hang undeva nu? pornesti in debug si cand vezi ca s-a hang-uit(face un ciclu infinit sau ceva) il opresti si vezi exact in ce functie/ciclu/linie/fisier e in executie. Este destul de important sa inveti sa faci debug. Eu unul nu m-as putea lipsi de el. Poti folosi visual studio pe windows, am crezut ca esti pe linux cand am vazut gcc(deaia am recomandat gdb) Quote
alex_cismas Posted January 25, 2012 Author Report Posted January 25, 2012 Am incercat si debugging si tot nu`mi dau seama ce are... Quote
alien Posted January 25, 2012 Report Posted January 25, 2012 Vezi sa ai EOF in fisierul care il citesti(apasa un enter la final) si sa fie windows format. Foloseste notepad++ sa satezi astea. Quote
alex_cismas Posted January 25, 2012 Author Report Posted January 25, 2012 nu inteleg ce zici, folosesc Fedora 16 nu windows, o versiune de linux. Quote
alien Posted January 25, 2012 Report Posted January 25, 2012 (edited) Incearca asta in loc de fgets(s,i,fid); - o sa opreasca while-ulif (!fgets(s,i,fid)) break;se pare ca !feof(fid) ala nu prea opreste while-ulsau ceva mai clean schimbi while-ul in:while (getc(fid))!=EOF) Edited January 25, 2012 by alien Quote
alex_cismas Posted January 26, 2012 Author Report Posted January 26, 2012 am rezolvat problema, voi reveni m`ai tarziu sa pun ce am modificat Quote