I used this code to use direct I / O instead of writing files, able to change instead of the format stream I want to be? Although I'm not sure how to do this, the help would be greatly appreciated.
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #Meder100 defined # FORMATREAD "% 12s% 100s% 100s" int main (zero) {FILE * in_ptr, * out_ptr; Four fname [max], lanm [max], number [12]; In_ptr = fopen ("phonelist.txt", "r"); Out_ptr = fopen ("history.txt", "w +"); If ((NULL == in_ptr) || (NULL == out_ptr)) {fputs ("Opening Files, Problems Removing Problems!", Stderr); Exit (1); } If (3 = fscanf (in_ptr, FORMATREAD, number, fname, lname)) {fprintf (out_ptr, "% s% s% s \ n", number, fname, lname); Printf ("% s% s% s \ n", number, fname, lname); Printf ("The phone book has format: name of the phone number \ n"); } And {fprintf (* out_ptr, "unknown type \ n"); Printf ("The phone book has an unknown format \ n"); } Fclose (in_ptr); Fclose (* out_ptr); Return 0; }
If there are any errors then I have a lot to regretfully coding.
To not use formatted output such as fprintf ()
, < Create a string in the code> sprintf () or other different ways in the code and write it. It is important that buffer
is large enough.
// fprintf (out_ptr, "% s% s% s \ n", number, fname, lname); // slightly oversize buffer, but it has% s format and variables in the form of four buffer [sizeof number + size fname + sizeof lname + size formatted]; Sprintf (buffer, "% s% s% s \ n", number, FNN, LN); Foots (buffer, out_pTR);
Concern about fscanf (in_ptr, FORMATREAD, number, fname, lname)
exists, which means line Formatted data FORMATREAD
does not detects '\ n'
as different from ''
. Recommend using fgets ()
to read the data line and then parse it.
The code also needs to improve buffer size and input format
# max 100 define formatted "% 12s% 100s% 100s" ... four fname [MAX + 1], lname [MAX + 1], number [12 + 1]; Good code is checking the retrun value from the input function, but instead of ==
using =
@WeatherVane If (3 = fscanf (in_ptr, FORMATREAD, number, fname, lname)) (3 == fscanf (in_ptr, FORMATREAD, number, fname, lname))
>
No comments:
Post a Comment