How do I move the file pointer to the next line in the file?

I am trying to write a function that reads line by line, stores each character into an array, manipulates that character array, prints the results in another file, and then moves on to the next line in the file.

Some I / O examples would look like this (the purpose of the program is to find a derivative, but this part of the code works fine.):

INPUT:
x
4x^4
91
sinx

OUTPUT:
1
16x^3
0
cosx

      

The function I have written so far:

int main(){

    FILE *inptr = fopen("functions.txt", "r");
    FILE *outptr = fopen( "derive.txt", "w");

    if(inptr)
        derive(inptr,outptr);

    return 0;
}

void derive(FILE *inptr, FILE *outptr){
    int i;
    char in = '0';
    char array[MAX];

    while((in = fgetc(inptr)) != EOF){
        for(i = 0; in != '\n'; i++){
            fscanf(inptr, "%c", &in);
            array[i] = in;
        }
        manipulate(array, outptr); // Function that finds the derivative and prints to output file
    }
}

      

My question is, how do I move the inptr file pointer to the next line?

+3


source to share


4 answers


while((in = fgetc(inptr)) != EOF){
    for(i = 0; in != '\n'; i++){
        fscanf(inptr, "%c", &in);
        array[i] = in;
    }
    manipulate(array, outptr); // Function that finds the derivative and prints to output file
}

      

You don't need to increment here inptr

, because since the fscanf()

in for loop is executing its pointer, it keeps increasing, so in the next loop you will be on the next line.




In the above code, you are missing the 1st character of any string ...

While you have read one character, but you do not use it, and in the next, for the loop, read the character again.

0


source


int main(){

    FILE *inptr = fopen("functions.txt", "r");
    FILE *outptr = fopen( "derive.txt", "w");

    if(inptr)
        derive(inptr,outptr);

    return 0;
}

void derive(FILE *inptr, FILE *outptr){
    int i;
    char in = '0';
    char array[MAX],word[MAX];
    fseek(inptr,0,SEEK_SET);
    while((in = fgetc(inptr)) != EOF){
        for(i = 0; in != '\n'; i++){
            fscanf(inptr, "%c", &in);
            array[i] = in;
            fgets(word,MAX,inptr); 
            /* this should set the cursur of inptr to the next line :D */
        }
        manipulate(array, outptr); // Function that finds the derivative and               prints to output file
    }
}

      



0


source


  How can I move the file pointer to the next line in the file?

Files are a collection of bytes, the meaning of which bytes depends on the file format.

"Plain text" is a group of files of different formats; with different character encoding methods (EBCDIC, ASCII, many variations of "extended ASCII", UTF-8, UCS-2, UTF-16, ...) and different ways of representing "end of line" ("\ n", "\ r \ n \ "," \ r ").

The first step is to decide whether your software will accept one particular variant of the "plain text" file format (and whether it will be corrupted for everything else - for example, when someone transfers a file from another operating system), or support multiple file formats with explicit controls. (with the / s command line argument so that the user can tell him what the file format is) and / or if he tries to auto-detect (for example, suppose UTF-8, which will work for ASCII as well, and then automatically detects that "end of line ", perhaps accepting" \ r "or" \ n "and then checking if \ n" follows "\ r" or "\ r" follows \ n ").

The next step is to convert characters from any file format that will be used to some "standard for you" character set (which may or may not be the character set used by the compiler) while discarding unnecessary ones (such as Unicode, for example). "byte order marks") and deals with the possibility of malformed / corrupted data (for example, a byte sequence that is not valid for UTF-8, a byte that is not valid for ASCII, ...) and deals with unwanted valid characters (NULL), CALL, DELETE, ...).

Immediately after "checking, converting and filtering the character set", one can do "end-of-line detection" (perhaps using a state machine to keep track of "the previous character was" \ r "" and "the previous character was" \ n ""; and maybe counting space characters and remove / remove all trailing spaces at the end of the line); and can store the character in an array for later use (if it has not been removed or "end of line") or call the function "process this line" (if it was "end of line"). Also don't forget the "end of file" - you can reach the end of the file while you are still in the middle of a line (and you can handle this by pretending that the last line in the file ends with "end of line" "when it is not).

Note that it is fscanf(inptr, "%c", &in);

extremely bad (you can spend most of your CPU on this function by parsing the format string "%c"

) and you can use it fgetc()

as a "less awful" alternative; and all of these functions ( fscanf()

, fgetc()

, fgets()

...) in any case, are generally unsuitable for use (unless you're doing unknown specific assumptions about the compiler what file format is actually a "plain text", and then broken) ... and wrong for everything else) and most of these features are slow. Instead; you want to consider usingread()

(so that an entire buffer full of bytes can be processed and avoid the overhead of C library functions and / or kernel API calls for each byte), or perhaps mmap()

.

Finally; you need to make sure that an attacker cannot (intentionally) provide a file that contains too many characters on a single line. A security check is required (for example if(i >= MAX) { // Array is full, can't add the next character to the array

); and may be accompanied by an error message ("Line is too long in the line number ...") or the use of a dynamically resizing array (for example, use a function realloc()

to increase the size of an array).

0


source


Use the function fgets()

to read from a file line by line.

-1


source







All Articles