tags:

views:

209

answers:

3
void cabclh(){
 FILE *fp;
 char *val, aux;
 int i=0;
 char *result, cabeca[60];
 fp=fopen("trabalho.txt","r");
 if(fp==NULL){
              printf("Erro ao abrir o ficheiro\n");
              return ;
              }

 val=(char*)calloc(aux, sizeof(char));
 while(fgetc(fp)=='\n'){
                fgets(cabeca,60,fp);
                printf("%s\n",cabeca);
                }
                fclose(fp);


                        free(fp);
                        }

void infos(){
 FILE *fp;
 char info[100];
 fp=fopen("trabalho.txt","r");
 if(fp==NULL){
              printf("Erro ao abrir o ficheiro\n");
              }
 while(fgetc(fp)=='-'){
                      fgets(info,100,fp);
                        printf("%s\n",info);
                        }
                        fclose(fp);

                        }   

At cabclh i want that the program recognize that the first line is header..but this code doesn't print nothing

At infos i want that he recognize that every lines that begin with '-' are info...

+3  A: 

You need to use a function to get a character from the file, such as fgetc(fp). You can't index into a file using the FILE pointer.

if (fgetc(fp) == '-') {
    ....
}
WhirlWind
Not working....
Pedro
void infos(){ FILE *fp; char info[100]; fp=fopen("trabalho.txt","r"); if(fp==NULL){ printf("Error\n"); } while(fgetc(fp)=='-'){ fgets(info,100,fp); printf("%s\n",info); } fclose(fp); } This doesn't work
Pedro
Include your actual code, and please explain what "not working" means.
WhirlWind
+1  A: 

there's a number of problems...

a simplistic approach to printing the first line...

FILE *fp;
int ch;
fp = fopen("trabalho.txt", "r");    
while( (ch = fgetc(fp)) != EOF) 
{
   if(ch == '\r' || '\n') break;
   putchar(ch);
}
/* printf("\r\n");  if you want this at the end, i */
fclose(fp);
Keith Nicholas
too few arguments to putc
Pedro
He likely meant to write `putchar(ch)`.
caf
A: 

First of all, your fp is a FILE*, thus it doesn't contain the file contents itself and cannot be used the way you did. In order to read file contents you need to use functions like fgetc, fgets, fread, and so on.

Secondly, in the first function you provided, the while loop isn't using a comparison, instead, it's assigning '\n' to fp, which isn't what you intended. For comparisons, use ==, !=, >=, and so on.

Also, your loop should be while (!(feof(fp) || ferror(fp))) { ... }, which means while not the end of the file or error. But below I explain the loop might not be needed in your case.

fgets() reads one line per time, including the \n character, or until the size you provided is reached (in fact, size-1, in this case, 59). So if want to read only the 1st line, and you know the length of the line you're reading, you don't need to use a loop.

In comments you asked how to read multiple lines, so you can do something like that:

char line[60]; // big enough to hold the entire line
FILE *fp = fopen("trabalho.txt", "r");
if (fp == NULL) { // check if the file was opened correctly
    printf("error opening the file\n");
} else {
    // while not end of file, and no error occured
    while (!(feof(fp) || ferror(fp))) {
        char *result = fgets(line, 60, fp); // read until \n, or 59 characters, which comes first
        if (result == NULL) { // if fgets returned NULL, it's an error
            printf("an error occurred\n");
            break;
        } else {
            if (line[0] == '-')
                printf("this line is info\n");
            printf("this line has: %s\n", line);
        }
    }
    fclose(fp);
}

However, don't expect it to work with lines longer than 59 characters.

jweyrich
thanks man...and about recognize the first chracter of the line?
Pedro
You can use `fgetc` to read one character a time for that, but if you need to "recognize" the first character for various lines, it's better to read the entire line and compare the 1st byte. In the code I wrote above, you could use `if (line[0] == '-') { ... }`. You're welcome.
jweyrich
it have to search in various lines...how fgetc?
Pedro
Updated my answer to illustrate. But don't expect it to work with lines longer than 59 characters.
jweyrich
thanks!!!it help a lot
Pedro
No problem. But consider using some online documentation, such as http://www.opengroup.org/onlinepubs/000095399/functions/fgets.htmlAnd when you have decided which answer is the most helpful to you, mark it as the accepted answer. All these informations are explained in the FAQ. You can read it here http://stackoverflow.com/faq
jweyrich