Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Spaces Added When Writing to File

    I have a script that reads two numbers (separated by a comma) from a txt file and adds one to one or both of the numbers. It then writes the new numbers back to the file, in the original format.

    It works, however, it keeps adding spaces to the beginning of the file. Normally this wouldn't be a major problem, but this file will eventually be modified a lot of times. That could eventually make the file size huge, slowing down the script.

    Here is the script (there's more to the PHP file, but this is what I'm having trouble with):
    PHP Code:
    // OPEN AND READ THE FILE
    $stats_file=fopen($stats_file_name,'r+');
    $stats=trim(fread($stats_file,filesize($stats_file_name)));

    // PARSE AND ADD 1 WHERE APPROPRIATE
    $stats_values=explode(',',$stats);
    $stats_values[0]++;
    if (
    $is_unique==true){
        
    $stats_values[1]++;
    }

    // WRITE NEW STATS TO FILE
    $new_stats=trim($stats_values[0].','.$stats_values[1]);
    $new_stats=str_replace(' ','',$new_stats);
    ftruncate($stats_file,0);
    fwrite($stats_file,$new_stats);
    fclose($stats_file); 

  • #2
    New Coder
    Join Date
    Mar 2006
    Location
    I'm lost, livin inside myself
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've made scripts before involving such things, make use of the trim() function before you write to the file, that should solve your issue.
    $guiness &= new sixpack();
    $guiness->chug();

  • #3
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by thesmart1 View Post
    PHP Code:
    $stats=trim(fread($stats_file,filesize($stats_file_name))); 
    PHP Code:
    $new_stats=trim($stats_values[0].','.$stats_values[1]);
    $new_stats=str_replace(' ','',$new_stats); 
    I used trim in both reading and writing. I even used str_replace to try to remove spaces manually in writing to the file.

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    check for new line character and start of line ......

  • #5
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I added $new_stats=str_replace("\n",'',$new_stats); and still got the same result.

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,947
    Thanks
    2
    Thanked 170 Times in 165 Posts
    You need to seek to the beginning of the file after the truncate.

    PHP Code:
    // OPEN AND READ THE FILE
    $stats_file=fopen($stats_file_name,'r+');
    $stats=trim(fread($stats_file,filesize($stats_file_name)));

    // PARSE AND ADD 1 WHERE APPROPRIATE
    $stats_values=explode(',',$stats);
    $stats_values[0]++;
    if (
    $is_unique==true){
        
    $stats_values[1]++;
    }

    // WRITE NEW STATS TO FILE
    $new_stats=$stats_values[0].','.$stats_values[1];
    ftruncate($stats_file,0);
    fseek($stats_file,0);
    fwrite($stats_file,$new_stats);
    fclose($stats_file); 

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,947
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Actually, the seek can be either before or after the truncate.

  • #8
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Awesome, it worked! Thanks!

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,947
    Thanks
    2
    Thanked 170 Times in 165 Posts
    I probably should correct my last statement. Given the small test I did, it didn't matter if the seek was before or after the truncate, but in actuality, it should be done prior to the truncate.

  • #10
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    It works before or after, but I do now have it before ftruncate.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •