![]() |
|
#1
|
|||
|
|||
Need help for write data into multiple filesHi Everybody,
I want to write data into multiple files say for example, I should write "HAI" into files like....... 1agent.dat, 2agent.dat, 3agent.dat and so on... please look at my following coding, Main part of the program as, CPP / C++ / C Code:
Here I included two funtions. there are, first is change intger to string CPP / C++ / C Code:
second is for rename the file, CPP / C++ / C Code:
I hope in my two functions there is no problem but problem in main program specially marked by red lines. I hope you could understand my reqirement or purpose. I am confused very much how to handle this problem. Pls help me according my purpose, how should I write my main program. Thanks lot in advance. Regards, Sitha. Last edited by LuciWiz : 18-Nov-2006 at 13:03.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: Need help for write data into multiple filesThe only thing i could find is in the intostring function, wheres the statement
result[n] += id; is not valid, restult varibale is string type and you trying to asign a int to it. Try this link http://www.experts-exchange.com/Prog..._20670737.html To find some info on how to convert int to string. |
|
#3
|
|||
|
|||
Re: Need help for write data into multiple filesQuote:
Here's what your program seems to be trying to do (Here I disregard issues of programming practices with std::string objects; I am just trying to follow your logic into the actual program): 1. Opens a file named "agent.dat" for writing (So it's a brand new, empty file. If there was previously a file named "agent.dat", it's destroyed.) 2. Calls a function named RenameFile that executes the following system call: "rename agent agent00" Note that if there is no file named "agent", the system call does nothing except create an error message. 3. Creates a string whose value is "00agent.dat" 4. Repeats steps 2 and 3 except instead of "00", it uses "01", "02", "03", ..., 10. 5. Closes the (still empty) file "agent.dat". Note that after the first time through the loop, if there had been a file named "agent", it has been renamed to "agent00", so all subsequent calls to RenameFile() result in a system call that is guaranteed to fail. Now, I respectfully suggest that you write down a sequence of steps that you really mean to execute (in English or other non-programming language) and then see what you need to do in your program to implement the requirements. Then try to write the code. Regards, Dave |
|
#4
|
|||
|
|||
Re: Need help for write data into multiple filesDear Dave,
Yeah I understood my logic is too bad for creating a multible files. Is there anyway to create multible files? For every loop a function should create a files. eg, Code:
for corresponding i value if my program create files my problem will be over. Pls help me for this regard. Thanks a lot. Regards, Sitha. |
|
#5
|
|||
|
|||
Re: Need help for write data into multiple filesQuote:
If you are going to have more than one file open at a time, you need to have as many ofstream objects as there are files. One way is to make an array of ofstreams: CPP / C++ / C Code:
You don't need to rename anything (so lose the RenameFile() function --- it's pretty ugly anyhow). Just create a new name for each ofstream and open the file: CPP / C++ / C Code:
Then use each one as you need to. For example: CPP / C++ / C Code:
And, to close them all: CPP / C++ / C Code:
Note that all operating systems have some limit on the number of files that are allowed to be open at any one time, so this may not work of you need 10000 files. (There may or may be an implementation limit for the compiler also. It's not always easy to find out what the limits are.) If you only need one file open at a time (like if you are going to write some particular thing to each file), then a single ofstream will serve. Just make a loop that repeatedly does the following: 1. Generate a new file name, using a base name and the current loop counter value. 2. Open the ofstream with that file name. 3. Write the information, or do whatever you need to do with that file. 4. Close the ofstream. Regards, Dave Last edited by davekw7x : 18-Nov-2006 at 10:28.
|
|
#6
|
|||
|
|||
Re: Need help for write data into multiple filesQuote:
The string was initialized to a number of zero chars, so result[n] is the char '0'. Adding an int whose value is greater than or equal to zero and less than or equal to nine gives the corresponding char. That is, an integer value of zero gives '0', an integer value of one gives '1', etc. (Sneaky, huh?) I might have put a some extra protection against using the routine with an invalid value for NMB_MCS, but the routine appears to work OK in this program. Actually I (probably) would have done it some other way entirely, but the point is that this routine is not creating problems in the given program. Furthermore it's a learning experience to look at things from a slightly different point of view. That's the main reason I keep coming back: I always learn something here (always). Regards, Dave |
|
#7
|
|||
|
|||
Re: Need help for write data into multiple filesHi,
Thank you so much for your kind help. Now it is working fine. Sure I want to deal with around 10000 of files and each file size may be around 200kb, not only for writing but also for reading I want to deal with 10000 of files. Pls look at my following code for reading of files and write into a temporary file so called driftdata. CPP / C++ / C Code:
Here pls tell me how to improve my coding as you suggested that at a time open a file and read data and close it. then open second file and so on. Also I want to read say first line, fifth line, 10th line like that I should skip 4 lines for reading. I did this in the red color coding. but it didnt work well and it produced blank lines as well in the output file. Pls help me for these both case. Last edited by LuciWiz : 19-Nov-2006 at 08:40.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#8
|
|||
|
|||
Re: Need help for write data into multiple filesQuote:
So, you need to close the stream inside the loop before opening it for the next file. Quote:
Then use a variable named lineno (or some such thing). Initialize it to zero each time you open a new file and increment it each time you read a line. Make some logic inside the loop that tells the program which lines to write. For example if you want to write line 1, 5, 9, 13, ... (Do the first one then skip 3 and write then skip 3 and write,...) the logic is ((lineno % 4) == 1). If the logic is more complicated, then do whatever you need: CPP / C++ / C Code:
Note the cout << statement that I put in for debugging purposes. Start by testing a directory with a small number of small files. Make absolutely sure that the program handles this case exactly the way you have in mind. (Look in the output file to make sure that the lines have been copied correctly.) Then you can omit or change the debugging printout to suit your needs. For example with a directory with two data files Code:
The first file had 11 lines; the second had 58. Regards, Dave |
|
#9
|
|||
|
|||
Re: Need help for write data into multiple filesHi Dave,
Thank you so much for your helping. What coding you have writen here is really I expected to do. Actually I am a Civil Engineer working on numerical analysis and GIS stuff. But my porgram knowledge is very limited So Pls suggest me how can I improve my skill in programing? And pls tell me how can I use Object Oriented technique for this type of programing. Regards, Sitha. |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| Strange C++ code memory leakage problem | gaoanyu | C++ Forum | 7 | 04-Nov-2005 08:09 |
| Need help reading data from files | blitzy | C Programming Language | 5 | 15-Mar-2005 19:07 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| using vector or array | oshiotse | C++ Forum | 4 | 16-Apr-2004 10:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The