I have a simple variable record length text file. For each record I entered manually to get started, they have a /cr and /n after line. I used notepad to create my file. I now have a PHP/HTML script which I want to be able to add, modify and delete records. I am currently working on the modify. I am using the fgets, ftell, fseek and fwrite to manage the file. When I write my record to the file and open it up in notepad, it doesn't create a new line. It appends it to the current line. Do I need to specify how many characters to write out in the fwrite? How can I insert the carriage return and newline characters so notepad will recognize them?
Thanks in advance for your help here. :queasy:

    Post your code with examples of what you are inserting, and how you are trying to accomplish this from within the code. (Usually when inserting into a text file, you should put your new line break as part of the text to insert, so there is that last blank line on the text file itself... That way, when you open to append later, you're starting on that new line. Either way, without the code, it'll be tough to help out.

      fwrite("A line of text\n");

      should do it.

        1848NC-CS,22113,80.00
        2935NK3,30025,1.00
        54P-CS,33050,10.00
        63P-CS,11332,20.00
        74P-CS,00000,000.00
        86P-CS,22334,1.00

        The above is my test file which is delimited by commas. I am matching on the first part of each record and then replacing the entire record when a macth is found with new data. The code I am using is:

        $f1 = fopen("c:\Metrolabels\TextFile\UPC.txt", "r+t");
        rewind($f1);

        if ( $Action1A == 'Modify' ) {
        $PartNbr = rtrim($PartNbr);
        while (!feof($f1)) {
        $holdf = fgets($f1);
        $part1a = explode(',',$holdf);
        $PartCmp = rtrim($part1a[0]);
        $pointr = ftell($f1);
        if ($PartCmp == $PartNbr) {
        $lengthf = strlen($holdf);
        $pointr = $pointr - $lengthf + 2;
        fseek($f1,$pointr);
        $holdrcd = rtrim($PartNbr) . ',' . rtrim($UPCode) . ',' . rtrim($Price);
        $lengthr = strlen($holdrcd);
        $holdf = $holdrcd;
        fwrite($f1, $holdf, $lengthr);
        break;
        }
        }
        }
        fclose($f1);

        After executing the above code, here is what I see in notepad:

        1848NC-CS,22113,80.00
        2935NK3,30025,1.00
        54P-CS,33050,10.00
        63P-CS,11332,20.0074P-CS,10024,10.00.00
        86P-CS,22334,1.00

        As you can see, the 74P-CS is not on its own line. This is where the newline/carriage return should come in handy.

          Maybe you could replace:
          fwrite($f1, $holdf, $lengthr); with
          fwrite($f1, $holdf . "\n");

            Thanks...The reply posted did provide me with a lot of help. The issue has been resolved.

              Write a Reply...