phplove Posted May 17, 2009 at 03:53 PM Report #264832 Posted May 17, 2009 at 03:53 PM viva mais uma duvida o que eu quero fazer e pegar numa string corta-la em duas mas antes tenho que testar se é o fim do fechei. o que eu cria era saber como posso avançar e retroceder linha no ficheiro? #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Sinst { char a[20]; char b[179]; }inst; inst cut(char f[]) { int j=0,i=0; inst a; while(f[j]!=' ') { a.a[j]=f[j]; j++; } a.a[j]='\0'; while(f[j]!='\0') { a.b[i]=f[j]; j++; i++; } a.b[i]='\0'; return a; } int main() { char line[200]; inst arg; FILE *f; f = fopen("pbm.sss", "rt"); if (f == NULL) { printf("Error operning file\n"); exit(1); } while (fscanf(f, "%s", line)!=EOF) { fgets(line, 199, f); arg=cut(line);printf("'%s' '%s\n'",arg.a,arg.b); } return 0; }
Ferreira Posted May 17, 2009 at 08:38 PM Report #264927 Posted May 17, 2009 at 08:38 PM Não podes simplesmente guardar todas as linhas que vais lendo para as poderes ler mais tarde? http://twitter.com/ferreira
phplove Posted May 17, 2009 at 09:51 PM Author Report #264968 Posted May 17, 2009 at 09:51 PM nao por que depois e para aplicar é para aplicar a if's
Darkblood Posted May 18, 2009 at 03:24 AM Report #264984 Posted May 18, 2009 at 03:24 AM O que tu queres parece-me que o teu programa já faz... Não é possível andar só uma linha para trás. O que é possível é andar o ficheiro todo para trás com o rewind(). Em vez de usares: while (fscanf(f, "%s", line)!=EOF) { fgets(line, 199, f); arg=cut(line);printf("'%s' '%s\n'",arg.a,arg.b); } Substitui por isto: while (line!=NULL) { if(fgets(line, 199, f)==NULL) break; arg=cut(line);printf("'%s' '%s\n'",arg.a,arg.b); }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now