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

Insert tab

Hi all,

I am reading from a text file and then write into an array line by
line. For that reason a writeline function has been created. When the
float numbers are written into the array I would like them to be
separated by a tab. Any idea how it might work? Thanks in advance.

Cheers,

Paul

Oct 13 '05 #1
8 3830
Amira wrote:
I am reading from a text file and then write into an array line by
line. For that reason a writeline function has been created.
Care to share it with us?
When the
float numbers are written into the array I would like them to be
separated by a tab. Any idea how it might work? Thanks in advance.


Yes, it will work just fine. When tab characters are inserted into the
stream between floating point numbers, the numbers will be separated by
those tabs.

V
Oct 13 '05 #2
--> here is the writenextvalue function ...

void writenextvalue( FILE* fh, double value, bool newline )
{
char* text = new char(20);
sprintf(text, " %lg ", value);

for (int i=0; i<strlen(text); ++i)
fputc(text[i], fh);
if (newline)
fputc(10,fh);
else
fputc(32,fh);
}

--> here the information is transfered and put into the array

for (int x=0 ; x<32 ;x++)
{
for (int z=0 ; z<16 ;z++)
{
for (int kx=0 ; kx<32 ;kx++)
{
for (int kz=0 ;kz<32 ;kz++)
{
realp[x][z]+=realpk[kx][kz]*cos(PI*2/32*(kx*x+kz*z));
imp[x][z]+=impk[kx][kz]*sin(PI*2/32*(kx*x+kz*z));

}

}

result[x][z]=sqrt(realp[x][z]*realp[x][z]+imp[x][z]*imp[x][z]);
writenextvalue( fh, result[x][z], false );
putc(\t);

}
writenewline(fh);
}

Oct 13 '05 #3
Amira wrote:
--> here is the writenextvalue function ...

void writenextvalue( FILE* fh, double value, bool newline )
{
char* text = new char(20);
sprintf(text, " %lg ", value);

for (int i=0; i<strlen(text); ++i)
fputc(text[i], fh);
if (newline)
fputc(10,fh);
else
fputc(32,fh);
What do those 'fputc' do? Why it's 10 and why it's 32? What do they
mean? Why don't you simply use 'fprintf'?
} [...]


V
Oct 13 '05 #4
because it is writing into a stream

Oct 13 '05 #5
Amira wrote:
because it is writing into a stream


I am sorry, which question are you replying to?
Oct 13 '05 #6
Amira wrote:
--> here is the writenextvalue function ...

void writenextvalue( FILE* fh, double value, bool newline )
{
char* text = new char(20);
sprintf(text, " %lg ", value);

for (int i=0; i<strlen(text); ++i)
fputc(text[i], fh);
if (newline)
fputc(10,fh);
else
fputc(32,fh);

Victor Bazarov wrote: What do those 'fputc' do? Why it's 10 and why it's 32? What do they
mean? Why don't you simply use 'fprintf'?

Amira <pa********@gmail.com> wrote: because it is writing into a stream


Amira,
Please quote properly when replying to posts.

Victor was asking why you are using such things as "10" and "32" instead
of symbolic names, like '\n' for newline and '\t' for tab.

Also, since fh is a FILE*, you should be able to use fprintf, like:

(not tested)
fprintf(fh, " %lg ", value);
if (newline)
fprintf(fh, "\n");
else
fprintf(fh, "\t");
Though personally I would prefer a C++ solution:

(also not tested)
void writenextvalue(std::ofstream& fh, double value, bool newline)
{
fh /* << formatting manipulators */ << value << (newline ? '\n' : '\t');
}

--
Marcus Kwok
Oct 13 '05 #7
Amira wrote:
--> here is the writenextvalue function ...

void writenextvalue( FILE* fh, double value, bool newline )
{
char* text = new char(20);
This is wrong, it allocates a *single* character which gets the value
20. You need square brackets. But really why don't you do it like this

char text[20];

Much simpler and it stops you having a memory leak later on.

sprintf(text, " %lg ", value);

for (int i=0; i<strlen(text); ++i)
fputc(text[i], fh);
if (newline)
fputc(10,fh);
else
fputc(32,fh);
}


john
Oct 13 '05 #8
Amira wrote:

A lot of good adivce given already, but
noone answered your first hand question

result[x][z]=sqrt(realp[x][z]*realp[x][z]+imp[x][z]*imp[x][z]);
writenextvalue( fh, result[x][z], false );
putc(\t);


\t is a character like any other character ('a', 'b', 'c', ... '0', '1', '\n', ... )
thus you need to enclose it in quotes

putc( '\t' );

But all of this looks mighty complicated for the simple task you are working on. Especially
having fucnctions that ... well, ... basically just call 1 function from the standard library.
Why not drop those and simply do:

for (int x=0 ; x<32 ;x++)
{
for (int z=0 ; z<16 ;z++)
{
for (int kx=0 ; kx<32 ;kx++)
{
for (int kz=0 ;kz<32 ;kz++)
{
realp[x][z]+=realpk[kx][kz]*cos(PI*2/32*(kx*x+kz*z));
imp[x][z]+=impk[kx][kz]*sin(PI*2/32*(kx*x+kz*z));
}
}
result[x][z]=sqrt(realp[x][z]*realp[x][z]+imp[x][z]*imp[x][z]);

fprintf( fh, " %lg\t", result[x][z] );
}
fprintf( "\n" );
}

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Howard Hinnant | last post by:
I recently asked for a survey of multimap insert with hint behavior, in support of a paper I'm writing concerning lwg issue 233. My sincere thanks to Beman Dawes, Raoul Gough, Russell Hind, Bronek...
6
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
3
by: MP | last post by:
Hi Posted this several hours ago to another ng but it never showed up thought i'd try here. using vb6, ado, .mdb, jet4.0, no access given table tblJob with field JobNumber text(10) 'The...
6
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN,...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.