Home > Back-end >  Write a program
Write a program

Time:12-02

In the main function of 10 lines of following form save to file test1-2. Dat, then enter an integer m (1 & lt;=m1
5 7
9, 11, 13
13 15 17 19
17 19 and 23 25
21 and 25 and 29 31
25 and 29, 31, 33, 35 37
29 and 33 to 35 37, 39 and 41 43
33 to 35 37, 39 and 41 and 45 47 49
37 and 41 and 45 to 47 49 51 and 55
Disp function features are: according to the value of the parameter n test1 - (2) from the file content and read from the dat before output n lines in accordance with the above form,
5 such as input, the results showed as follows:
1
5 7
9, 11, 13
13 15 17 19
17 19 21 23 to 25

CodePudding user response:

# include
Void disp (int n)
{int I, j, k;
FILE*fp;
Fp=fopen (test1-2. "dat", "rb");
for(i=0; i{
for(j=0; j<=I; J++)
{
K=getw (fp);
Printf (" % d \ t ", k);
}
printf("\n");
}
fclose(fp);
}


Void main ()
{
Int I, j, k, n;
FILE*fp;
Fp=fopen (test1-2. "dat", "wb");
for(i=0; i<10; I++)
{
for(j=0; j<=I; J++)
{k=I * 4 + 1 + j * 2;
Putw (k, fp);
}
}
fclose(fp);
The scanf (" % d ", & amp; N);
Disp (n);
}

CodePudding user response:

 # include & lt; Stdio. H> 
Void disp (int);

Void disp (int n)
{
Int line=0;//line: line counter
Char ch;
FILE*fp;
Fp=fopen (test1-2. "dat", "r");//open the text file
While (linePutchar (ch);//show read characters
If (ch=='\ n')//if read \ n the on line count
line++;
}
fclose(fp);
}

Int main (int arg c, char * * argv)
{
Int I, j, k, n;
FILE*fp;
If ((fp=fopen (" test1-2. Dat ", "w"))==NULL) {//use text files to save raw data
Printf (" Open file error. ");
return 0;
}
for (i=0; i<10; I++) {//I specified format generated file
for (j=0; j<=I; J++) {
K=I * 4 + 1 + j * 2;
Fprintf (fp, "% 4 d", k);
}
Fprintf (fp, "\ n");
}
fclose(fp);
The scanf (" % d ", & amp; N);
Disp (n);
return 0;
}

Please see if the above procedure meet the requirements of your topic,

CodePudding user response:

Don't post code, said the train of thought, first call line according to read the file in the C API (no this API is a character in a character read know encounter \ n), where the number of rows, just read a character is a character, separated by Spaces
  • Related