c - Check the next line -
I have a file that has many client records formatted like:
customer ... Details .. Details ... Customer ... Details ... Details ... Details ... Customer .... .
I need to split the file into two. Currently, I count all the rows in the file and divide it by 2
, and then check Take that if it starts with the line string the customer
If this happens, how can I print all the rows before the customer?
And
If this does not happen, how can I check that the next line starts with Customer
my code :
int main (int argc, char * argv []) {char ioarea [1000]; Int TotCustLines = 0, i = 0; Int reqLine = 0; Int getLine = 0; If ((inFile = fopen (argv [inFileName], "r")) == NULL printf ("failed to open the input file"); Whereas (fgets (ioarea, BUFFER_SIZE, infile)) {TotCustLines ++; }} If (Tostastlines / 2 == 0) Reclline = (TotestLines / 2); Else reqLine = (TotCustLines / 2) +1; Fseek (infile, 1, SEEK_SET); While (fgets (ioarea, BUFFER_SIZE, infile)) {getLine ++; If (getLine == reqLine) {if (strncmp (getLine, "client record", 15) == 0) // How to write all the rows before the customer record // If not, how can I check the next line}}
Please suggest a solution I am new to the C file concept.
Find the file you can use to read, then search
1) Get the file size (use fseek ()) / ftell ()
or example 1 to get the line count, and the longest line.
2) Create and assign memory for an array of strings, char ** buffer;
See Example 2
3) File using index by means of fgets ()
, buffer [index]
Read in the array. 4) Array buffer [] now in the index file in the line
Now you can see the strstr ()
string comparison You can use the function.
Example 1 Route to get the count of rows in the file, and the longest line (to create memory)
Int Cnt = 0; Int len = 0; Int keep = 0; Four lines [260]; // == MAX_PATH_LEN In windows, select the long value if necessary (fgets (line, 260, fp)) // file (len = strlen (line); // each line gets track of each row in the length of the track If you keep (for lane & gt; keep) = len; // Keep only the longest line will be the longest line value to keep cnt ++;} //, the number of lines in cnt will be < / Code>
Example 2 Create memory for an array C string ( char ** string;
):
Note: Create and call example of calling function:
char ** variable = {0}; Int CNT, MaxLen; Variable = allocMemoryStr (variable, & amp; cnt, and maxlen);
........
char ** allocMemoryStr (core ** strings, int newsreadings, intex line) {int i; strings = calloff (numStrings, sizeof (char *)); // (i = 0; i & lt; numStrings; I ++) {String [i] = make points for arrays for callers (maxlane + 1, sizeoff (four); // Create memory for each array} return string; }
Comments
Post a Comment