Jump to content

[r/w text files] Way easy text file problem


klyxmaster

Recommended Posts

Ok i have been working on this for a day+ now. here is my delema

 

simple .ini text file. when a user makes a change (via html form) it makes the correct adjustments.

problem is the newline issue

 

1. if i put a "\n" at the end (when using fputs) works great, except everytime they edit the file it keeps adding a new line (i.e. 10 edits there are now 10 blank lines!!!!)

2. if i leave off the "\n" it appends the next "fgets" to that lilne making a mess

 

##-- Loop thruoght the ORIGINAL file
    while( ! feof($old))
    {
        ##-- Get a line of text
        $aline = fgets($old);
        
        ##-- We only need to check for "="
        if(strpos($aline,"=") > 0 )
        {
            ##-- Write NEW data to tmp file
            fputs($tmp,$info[$i]." = ".$rslt[$i]."\n");
            $i++;
        }
        ##-- No Match
        else
        {
            fputs($tmp,$aline."\n");
        }//Checking for match
    }//while eof(old)
    

 

what in the world is making this such a big deal. i dont remember having this issue in the past

I tried opening with w+, and just w on the temp file

 

a typical text line would be

 

some fieldname = some value

 

the scipt cycles through the file ignoring comments that are "#"

 

 

ps

the tmp file will overwrite the origianl once complete

 

all i really want to know is WHY i cant get the newline to work, and what is the suggested fix

 

EDIT:

i just tried PHP_EOL and it still appends another newline

Link to comment
Share on other sites

OK, Im wayyyyyyy too impatient (which can be a good thing )

I kept looking and tried a different method. the following seems to work perfectly and its shorter!!

 

function updateTextRec($orig_filename,$data)
{
    ##-- Put the $orig_filename in an array
    $data_file = file($orig_filename);
    
    ##-- Loop through the new array
    foreach($data_file as $line_num => $aline)
    {
        ##--Cycle through the user $data for a match
        foreach($data as $field => $value)
        {
            $field = str_replace("_",".",$field);
            if(strstr($aline,$field))
            {
                $data_file[$line_num] = $field." = ".$value."\n";
            }
        }
    }
    
    ##-- Save it
    $save_file = implode("",$data_file);
    $fp = fopen($orig_filename,"w");
    fwrite($fp,$save_file);
    fclose($fp);

 

Original idea from :  http://php.bigresource.com/Track/php-G1NzcoJB/

Link to comment
Share on other sites

I think the problem you are experiencing is that fgets() will get a line from the file and that line already has a line break on the end. In the IF condition you are rewriting the line and including a line break on the end. But, in the ELSE condition you are appending a line break to the original line (which already had a line break).

 

However, the problem is further exacerbated by the fact that fputs() also put in a line break - but you won't see it in a plain text editor. If you were to open the file using Word or Wordpad you would see the extra line breaks. To be honest, I don't really get it - but I think it boils down to a difference on how Linux/Windows define line breaks. I've run into this problem in other instances and had to fight through to resolve it. But the solution to your problem is just to write better code.

 

1. It is inefficient to write one line at a time. Instead, generate all the content for the new file THEN, when complete, replace the original file. You don't need a temp file at all. Just use a temp string.

2. The ELSE condition is not necessary. If the IF condition is true then redefine $aline. Then you only need one line to handle the disposition of the content.

3. Remove the "internal" line breaks using trim() then add your line breaks.

 

Personally, I would use file() to break the content into an array, redefine the values that need to be redefined, then implode the result and overwrite the file. But, using your method, I would rewrite as follows:

 

##-- Loop thruoght the ORIGINAL file
    while( ! feof($old))
    {
        ##-- Get a line of text
        $aline = trim(fgets($old));

        ##-- We only need to check for "="
        if(strpos($aline, "=") > 0 )
        {
            $aline = "{$info[$i]} = {$rslt[$i]}";
            $i++;
        }
        $new .= $aline . PHP_EOL;
    }//while eof(old)
//Close file handle for reading
fclose($old);

//Reopen file in write mode
$old = @fopen($file, "w");
fputs($old, $new);
fclose($old);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.