Problems about file operations in C

xiaoxiao2021-03-06  25

Main topic: About the problem of FM in C! Please help me. Servers: Superxxm (Troy) Isometric: 100 Community: C / C C Language Problem Click: 50 Reply Number of people: 14 Posted: 2005-1-18 22:17:31

The younger brother has recently been operated in the documentation of C. I am a rookie, I don't understand. Can experts help me? For example, there are several numbers in a file, those numbers are separated by spaces, the number of spaces is not limited, how to sort them, then save them to another file, in general, I am in the file Digital processing is a bit not understanding, please help, the younger brother is not much, you will give 50 points.

Reply to: Jamesfancy () James Fancy () Reputation: 100 2005-1-18 22:34:26 Score: 0

There are two ways to read out.

1. One line of reading, it seems that there is a function of dedicated reading line, and I don't know much. After reading it by the line, use STRTOK to put the string of the string in empty. Another one is in another file, with a return interval.

2. A word a word, read it, put it in a buffer until the space is read. Then write the scrubber in the list or in another file, then output a carriage. Then read the space and read the numbers from the head to the buffer, loop.

TOP

Reply to: Superxxm () Reputation: 100 2005-1-18 23:15:58 Score: 0

I am still confused. Men can help me write some main part. Let me see? ?

TOP

Reply to: lovefly_fanny (love waterfish) () reputation: 100 2005-1-19 1:23:00 Score: 0

Under the case of comparison, you can open up a buffer first.

Use fscanf () to read the characters in the file one by one,

At the end of the space, put into the buffer (if it is int, or float,

More easily handled, directly converted into integers or float directly into an integer or float, in place

Just sort (this is also better implementation), and finally put this piece directly

Buffering back to a new file, you can use fwrite ()

TOP

Reply to: Imrainman () Reputation: 97 2005-1-19 1:58:36 Score: 0

#include

#include

void main ()

{

INT A, B, C;

FILE * f = fopen ("1.txt", "r");

FSCANF (F, "% D% D% D", & A, & B, & C); // fscanf will automatically remove excess spaces and newline characters

Printf ("% D% D% D", A, B, C);

Fclose (f);

}

TXT

1 2 3

Output results:

1 2 3

TOP

Reply to: stevens2009 (wind) () Reputation: 99 2005-1-19 9:09:04 Score: 0

Mark

TOP

Reply to: Superxxm (Troy) () Reputation: 100 2005-19 11:00:52 Score: 0IMRAINMAN Master, if you are multi-line, then the number of uncertainties? ?

TOP

Reply to: Suyouxin (Why is there a boating without pulping) () Reputation: 100 2005-1-19 11:09:45 Score: 0

Take a row

#include

Void main (void)

{

File * fp;

Char line [100];

IF ((fp = fopen ("2.txt", "r")))! = null)

{

IF (FGETS (Line, 100, FP) == NULL)

Printf ("FGETS ERROR / N");

Else

Printf ("% s", line);

Fclose (fp);

}

}

document content:

Dasdasdasda

DS12122

1231

Output

Dasdasdasda

DS12122

1231

Multi-line plus a while

While (fp! = EOF) {

IF (FGETS (Line, 100, FP) == NULL)

Printf ("FGETS ERROR / N");

Else

Printf ("% s", line);

}

TOP

Reply to: 0909424 (Raw) () Reputation: 100 2005-19 12:06:07 Score: 0

Read it in a temporary file, then write to another file

TOP

Reply to: Well2008202 (Well_2008202) () Reputation: 100 2005-1-19 13:02:30 Score: 0

sorry

I am just learning, I still don't understand, I just have a look, try to see

TOP

Reply to: Piziliu2003 (Tears Coffee) () Reputation: 100 2005-1-20 17:29:30 Score: 0

Give you two functions read, write it.

.

Int readdat ()

{

File * fp;

INT i = 0;

UNSIGNED Char * P;

IF ((fp = fopen ("dmi.txt", "r"))) == null) Return 1;

While (FGETS (XX [I], 80, FP)! = NULL)

{

P = strChr (xx [i], '/ n');

IF (p) * p = 0;

i ;

}

Maxline = i;

Fclose (fp);

Return 0;

}

Void WriteDat (Void)

{File * fp;

INT I;

FP = fopen ("Dmi.txt", "W");

For (i = 0; i

{

FPRINTF (FP, "% S / N", XX [I]);

}

Fclose (fp);

TOP

Reply to: Imrainman () Reputation: 97 2005-01-22 11:11:00 Score: 0

TO: Superxxm (Troy)

There is no relationship. If there are many rows, Fscanf is automatically removed from the intermediate wrap and space.

#include

#include

void main ()

{

INT fendpoz = 0; int Fcurpoz = 0;

FILE * f = fopen ("1.txt", "r");

If (! f) return; / / Check if the file is open

FSeek (f, 0, seek_end); // Move the file pointer to the end of the file

Fendpoz = ftell (f); // Get the current file pointer

FSeek (f, 0, seek_set); // Move the file pointer to the beginning of the file

While (Fcurpoz

{

INT I;

Fscanf (f, "% d", & i); // fscanf automatically removes excess spaces and newline characters

Printf ("% d / n", i);

Fcurpoz = ftell (f); // Take the current file pointer

}

Fclose (f);

}

TXT

1 2 3

4

5 6

Output results:

1

2

3

4

5

6

TOP

Reply to: xuelong_zl () () Reputation: 100 2005-01-22 14:46:00 Score: 0

Mark

TOP

Reply to: fengyingLOIS (air shadow) () Reputation: 100 2005-01-22 18:10:00 Score: 0

Www.xinxi168.com

TOP

Reply to: yfy (Tian Yi) () Reputation: 100 2005-01-23 00:39:00 Score: 0

fscanf () function.

If you know that there are several numbers to read each line, you can read the numbers directly.

If you don't know how many numbers per row, you first read a string, traverse the string, remove the digital storage

Temporary string cache, get numbers with ATOI () or ATOF ().

TOP

转载请注明原文地址:https://www.9cbs.com/read-65456.html

New Post(0)