Jump to content

avançar e retroceder linha no ficheiro


Recommended Posts

Posted

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;
}
Posted

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);           
 } 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.