请帮我解释一下这两个程序。

u010179812 2013-06-19 09:42:55
书中有一道题,题目是:

编写一个程序,打开一个文本文件,文件名通过交互方式获得。建立一个循环,请求用户输入一个文件位置。然后程序打印文件中从该位置开始到下一换行符之间的部分。用户通过输入非数字字符来终止输入循环。

我是这样写的:

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define SIZE 100
int main(void)
{
FILE *fp;
char filename[SIZE];
char str[SIZE];
long pos, last;
int i;

printf("Please input the file's name:");
gets(filename);
fp = fopen(filename, "r+");
if(fp == NULL)
{
fprintf(stderr, "Can not open %s.\n", filename);
exit(1);
}
fseek(fp, 0L, SEEK_END);
last = ftell(fp);
rewind(fp);
scanf("%d", &i);
while(i>=0 && i<last)
{
pos = (long)i*sizeof(char);
fseek(fp, pos, SEEK_SET);
fgets(str, SIZE-1, fp);
fputs(str, stdout);
scanf("%d", &i);
}

if(fclose(fp) != 0)
{
fputs("Failing in closing the file.", stdout);
exit(2);
}
return 0;
}


假如原来的文件中第一行有字符abc 第二行有字符def 第三行有字符opq
我在黑窗口中输入10,得到的结果是opq,我想是不是第一行后面是不是有字符'\r''\n'?否则不会是这个结果,或者是我的程序有误?

另外,我在网上找到一个标准程序,有一步我没有看懂,就是程序中的两次row--以及一次column--,为什么要两次row--,这个程序好像是定位某行某列的字符,与书上的要求有点出入,是吗?程序如下:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX 81

int main(void)

{
char name[30],content[MAX];
int row,column;
FILE *fp;
printf("input the name of file:");
gets(name);
if( ( fp=fopen(name,"r") ) == NULL )
{
printf("Can't open %s",name);
exit(1);
}
printf("input the row and column to output:");
while ( scanf("%d%d",&row,&column) == 2 )
{
row--,column--;
fseek(fp,0,SEEK_SET);
while (row--)
{
fgets(content,MAX,fp);
}
fseek(fp,column,SEEK_CUR);
fgets(content,MAX,fp);
printf(content);
printf("input the start position to output:");
}
printf("Quit\n");

return 0;
}


...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-06-19
  • 打赏
  • 举报
回复
仅供参考
//问题:
// 1.打开一个文本文件。
// 2.查找这个文本文件的第r行的第c1-c2列?
//
// 如:
// 文件:a
// 11111111
// 22222QQQ
// 33333333
//
// 查找第2行,第6-8列。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXC 16000
char ln[MAXC];
FILE *f;
int r,c1,c2,n,L;
void getc1c2(char *fn) {
    ln[c2]=0;
    printf("Text at row(%d),column(%d..%d) of file %s is [%s]\n",r,c1,c2,fn,ln+c1-1);
}
int main(int argc,char **argv) {
    if (argc<5) {
    HELP:
        printf("Usage:\n    %s filename.ext r c1 c2\nto get text at row(r),column(c1..c2) of filename.ext\n",argv[0]);
        return 1;
    }
    r=atoi(argv[2]);
    c1=atoi(argv[3]);
    c2=atoi(argv[4]);
    if (r<=0 || c1<=0 || c2<c1 || c2>=MAXC) goto HELP;
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not open file %s!\n",argv[1]);
        return 2;
    }
    n=0;
    while (1) {
        if (NULL==fgets(ln,MAXC,f)) break;
        L=strlen(ln);
        if ('\n'==ln[L-1]) {
            memset(ln+L-1,0,MAXC-L+1);
            n++;
            if (r==n) {getc1c2(argv[1]);break;}
        } else {
            n++;
            if (r==n) {getc1c2(argv[1]);break;}
            printf("Warning: line %d too long(>%d characters)! Ignore tails.",n,MAXC);
            while (1) {
                if (NULL==fgets(ln,MAXC,f)) goto END;
                L=strlen(ln);
                if ('\n'==ln[L-1]) break;
            }
        }
    }
    END:
    fclose(f);
    return 0;
}
//C:\test\Debug>frcc a 2 6 8
//Text at row(2),column(6..8) of file a is [QQQ]
//
你怎么了熊吉 2013-06-19
  • 打赏
  • 举报
回复
1.应该就是\r\n的问题了,但是你确定它要求输入的文件位置是一个数,而不是第几行+第几列? 2.下面这个程序的话,其实就是输入第几行和第几列,确定这个位置,然后打印文件中从该位置开始到下一换行符之间的部分。 while(row--)其实是在倒数计数 比如你row是3,就会循环三次,每次执行一下fgets(content,MAX,fp),这个不是为了读取什么,纯粹为了移动文件指针到下一行 也就是这个while循环结束之后,你的文件指针就指在你要的那一行了 接下来是列,就是文件指针往右移column个位置 现在文件指针所在的位置,就是需要的位置了 回过头来说前面,为什么要row--,column-- 很简单,因为文件指针一开始在第1行,如果你要的是第1行,需要移动0次,你要第2行,需要移动1次
u010179812 2013-06-19
  • 打赏
  • 举报
回复
辛苦楼上的两位,明白了。我想应该是确定某行某列,我理解错误了。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧