473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

open file in read and write mode..w+

There is big or problem in open file in read and write mode..
Expand|Select|Wrap|Line Numbers
  1. $file = "data.txt";
  2.  
  3. $fp = fopen($file,"w+");
  4. $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes
  5. echo $line;
  6. fclose($fp);
  7.  
coding is not problem, The problem is file modifier as mention in PHP
use " w+ " for read the data in file and then overwrite the new data.
but it is not read the date in file already , while open the file it delete the data in file and then write to it.

so for read data i had used to open file two times first to read and then with w+ for over write...


>>and other is "r+" file modifier mention in php read and write, it does but it append the new data to file , which i do not need i need read the data from file and overwrite the new data to file...but w+ is also not worked..


Please tell the soluion how to solve this..

Thanks
Sep 15 '06 #1
2 12575
pbmods
5,821 Expert 4TB
Heya, agphoto.

Try reading the contents of the file using file_get_contents() (PHP: file_get_contents - Manual) and then doing a separate fopen(..., 'w') to overwrite the file contents.
Jan 24 '09 #2
nathj
938 Expert 512MB
Alternatively if you want the file open for read and write but don't want the contents overwritten then try opening the file using the 'a+' mode.

fopen manual

This should sort you out, if not then pbmods is the one to follow.

Cheers
nathj
Jan 26 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alex | last post by:
Rossum's tutorial on Python states: "open() returns a file object, and is most commonly used with two arguments: 'open(filename, mode)' mode 'r+' opens the file for both reading and writing." ...
0
by: Mark Phillips | last post by:
Hello, I am having a problem in which a file can get stuck open when a thread that is attempting to write to it gets aborted (ThreadAbortedException occurs). The log file gets stuck open until...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
2
by: tubby | last post by:
Does a py script written to open and read binary files on Windows affect files on a Linux or Mac machine in a negative way? My concern is portability and safety. I want scripts written within...
9
by: Robin | last post by:
// Pls note that test.txt exists. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fs("test.txt", ios_base::in | ios_base::app);
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.