简体   繁体   English

C编程:从输入文件中打印最后一行时出错

[英]C programming: Error printing last line from an input file

I want to print an integer from 6th column of last line of an input file "out". 我想从输入文件“ out”的最后一行的第6列打印一个整数。

Following is my code: 以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int len = 0;
    int n2 = 0;
    int n3 = 0;
    char line[1000];
    char *pch;
    char c[6] = "ATOM ";
    FILE *fp = fopen("1PGB.pdb", "r");
    FILE *op = fopen("out", "w");

    if (fp == NULL || op == NULL) {
        fprintf(stderr, "Error opening file.\n");
        exit(1);
    } else {
        while (fgets(line, sizeof(line), fp) != 0)
            if ((pch = strstr (line, c)) != NULL)
                fprintf(op, "%s\n", line);
    }
    fclose(fp);
    fclose(op);
    FILE *ip = fopen("out", "r");
    fseek(ip, 1, SEEK_END);
    if (fgets(line, len, ip) != NULL)
        puts(line);
    else
        printf("Error\n");
    fclose(ip);
}

It is expected to return the last line of the file "out". 期望返回文件“ out”的最后一行。

It gives output: Error 它给出输出:错误

The last few lines of the file out are: 文件的最后几行是:

 ATOM    427  N   GLU A  56       7.248   9.043   7.175  1.00 16.36           N  

 ATOM    428  CA  GLU A  56       6.283   8.177   6.480  1.00 19.22           C  

 ATOM    429  C   GLU A  56       6.780   7.744   5.081  1.00 22.26           C  

 ATOM    430  O   GLU A  56       7.521   8.520   4.401  1.00 23.58           O  

 ATOM    431  CB  GLU A  56       4.960   8.864   6.307  1.00 19.26           C  

 ATOM    432  CG  GLU A  56       4.093   8.873   7.512  1.00 19.10           C  

 ATOM    433  CD  GLU A  56       2.702   9.417   7.201  1.00 18.54           C  

 ATOM    434  OE1 GLU A  56       2.544  10.440   6.499  1.00 18.16           O  

 ATOM    435  OE2 GLU A  56       1.737   8.791   7.641  1.00 20.42           O  

 ATOM    436  OXT GLU A  56       6.410   6.617   4.667  1.00 24.74           O  

And I want to print the integer "56" in 6th col of the last line. 我想在最后一行的第6列打印整数“ 56”。

output file omitted: 输出文件省略:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int len = 0;
    int n2 = 0;
    int n3 = 0;
    int result=0;
    char line[1000];
    char lastline[1000]="";
    char *pch;
    char c[6] = "ATOM ";
    FILE *fp = fopen("1PGB.pdb", "r");
    //FILE *op = fopen("out", "w");

    if (!fp) {
        fprintf(stderr, "Error opening file.\n");
        exit(1);
    } else {
        while (fgets(line, sizeof(line), fp) != 0)
            if ((pch = strstr (line, c)) != NULL){
                strcpy(lastline,line); //save content of the last output
                printf("%s", line);
            }
    }
    fclose(fp);

    if(*lastline){
        strtok(lastline," "); //skip first field
        for(int i=0;i<4;i++) // skip next 4 fields (5 in total)
            strtok(NULL," ");
        sscanf(strtok(NULL," "),"%d",&result);//get and convert the 6th field
        printf("LastLine: %d\n",result); //print it
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM