69,369
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}
//问题:
// 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]
//