473,322 Members | 1,714 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,322 software developers and data experts.

Changing columns when writing data to text files

1
Hi guys,

I'm new here and just getting into C++ and would really appreciate your help.

I would like to know how to change columns each time i wanna write new data to a text file.

For e.g i have a nested loop such as:
Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<5,i++)
  2. {
  3.    data=100*`i;
  4.  
  5.    for (int s=0;s<20,s++)
  6.    {
  7.       char Name[30];
  8.       sprintf(Name,"Name%d.txt",s+1)
  9.       ofstream tfile( Name,ios::app);
  10.       tfile << data;
  11.    }
  12. }
  13.  
Therefore for each value of i, i want to write data into a new column in the corresponding text file.

Please advise.

Surosh.
Aug 20 '07 #1
1 3526
weaknessforcats
9,208 Expert Mod 8TB
for (int i=0;i<5,i++)
{
data=100*`i;

for (int s=0;s<20,s++)
{
char Name[30];
sprintf(Name,"Name%d.txt",s+1)
ofstream tfile( Name,ios::app);
tfile << data;
}
}
This code declares the ostream inside the loop. Declare it outside the loop. Every << will be a new column. Use a << '\n' for a new line.

You will need to separate the columns so you can read the file at some future point.

Expand|Select|Wrap|Line Numbers
  1. ofstream tfile( Name,ios::app);
  2. char Name[30];
  3.  
  4. for (int i=0;i<5,i++)
  5. {
  6.    data=100*`i;
  7.  
  8.    for (int s=0;s<20,s++)
  9.    {
  10.             sprintf(Name,"Name%d.txt",s+1)
  11.             tfile << data << '|';      // the | is a column delimiter
  12.    }
  13. }
  14.  
Personally, I would code:
Expand|Select|Wrap|Line Numbers
  1. ofstream tfile( Name,ios::beg);
  2. for (int i=0;i<100;i++)
  3. {
  4.    tfile << "Name" <<  i << ".txt|";
  5.  
  6. }
  7.  
Aug 20 '07 #2

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

Similar topics

7
by: kah | last post by:
How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in
10
by: Neil Trigger | last post by:
Is there a way of creating a seperate text file on a server every time a form is sent? -- ¿ Trigger ? http://www.magic2k.com/ http://www.oddmap.co.uk
55
by: Ton den Hartog | last post by:
Stupid basic question but I find it horribly imposible to find the answer elsewhere... :-( I want to have a piece of text in my HTML page and want to be able to change it in a Javascript...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
10
by: Rigs | last post by:
Hi, I have a textbox with a Custom Validator that utilizes the OnServerValidate method for that textbox. This works fine, however the method only executes when data exists in that textbox after...
0
by: Bob Smith | last post by:
I have existing word documents that we need to have the data document reflect whether we're running in development, test or production. The data header is longer than 255 characters, so I cannot...
1
by: Blasting Cap | last post by:
I am having trouble changing the font for a PushButton control in a datagrid button column. I have seen several posts refer to styles and simple changes to the HTML for font changes but most of...
0
by: projektsilence | last post by:
Hello all... I'm new here so bare with me please. I need to change collation type for a ton of tables and columns. I found a great site that showed me how to generate the sql to do this using a sql...
7
oll3i
by: oll3i | last post by:
i want to change the values in two columns one colum is a combobox and the secons column is editable too i want to get the value of that second column and the value of combobox and sent that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.