473,883 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to count rows and columns of integers/doubles in a file?

Hi,

I have some files which has the following content:

0 0 0 0 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0

I'm making a function for a bigger program, that

1) counts number of columns in each row
2) verifies that there are the same number of columns in each row
(if not: error + quit)
3) increments row count after one row is finished
4) stops execution if it encounters anything that is not an
integer (later: should also work for doubles)

My only problem is that I don't know how to switch line (carriage
return) or how to detect it using scanf. Besides that, the
program is almost finished. See below:
- - - - - - - - - - - - - - - -
#include <stdio.h>
void int_getdata(cha r *filename, unsigned int *x, unsigned int
*y);
int main()
{
unsigned int n_x[3], n_y[3];

int_getdata("un knowns.dat", &n_x[0], &n_y[0] );
int_getdata("BC _types.dat", &n_x[1], &n_y[1] );
// double_getdata( "BC_values.dat" , &n_x[2], &n_y[2] );

return 0;
}

void int_getdata(cha r *filename, unsigned int *x, unsigned int
*y)
{
FILE *fp;

unsigned int old_nx;
int int_readvalue, returnvalue;
//double double_readvalu e;

old_nx = 0; /* reset */
*x = 0;
*y = 0;

if ( (fp = fopen(filename, "r") ) == NULL)
{
printf("Cannot open file %s.\n", filename);
system("PAUSE") ; /* give the user at chance to see this
error before the windows shuts down */
exit(1);
}

/* get nx and ny */
do{
returnvalue = fscanf(fp, "%i", &int_readvalue) ; /* all
input must be valid */

if(returnvalue == 1) (*x)++; /* nx is getting larger */
else{
printf("Error: non-valid input found in %s.\n",
filename);
exit(1);
}

if(int_readvalu e == '\n')
{
if(old_nx != 0 && old_nx != *x)
{
printf("ERROR: nx is not a constant in file
%s!\n", filename);
exit(1);
}
else /* we should now be sure that nx is constant and
move on to the next input line */
{
old_nx = *x;
y++; /* ny is getting larger */
*x = 0;
}
}
} while(returnval ue != EOF);

printf("\nFinis hed reading from file %s: (x,y) = (%i,%i).\n",
filename, *x, *y);
fclose(fp); /* close input file, finished reading values in
*/
}

- - - - - - - - - - - - - - - -

I hope somebody can help me solve this small problem, so I can
finish the program....

I also get these small warnings, but they are not critical:

warning C4013: 'system' undefined; assuming extern returning int

warning C4013: 'exit' undefined; assuming extern returning int
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

Mar 15 '06
68 6851
Ben Pfaff wrote:
Mark McIntyre <ma**********@s pamcop.net> writes:
CBFalconer <cb********@yah oo.com> wrote:
Martin Jxrgensen wrote:

void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count)
{

void getdata(char *filename, /* room to describe */
unsigned *pCols, /* the various parameters */
unsigned *pRows, /* if desired */
int *count)


personally I consider this an abomination. The first form is way
more readable.


I work for a company that has the latter as required style.
(If you violate it you get scorched in code reviews.)


As usual, it depends. If there are many parameters, the second
style is almost mandatory. If very few, and the parameter names
are descriptive, the first is preferable. A mongrel is useful to
group associated parameters, such as array pointer and array size.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>

Mar 19 '06 #51
On Sun, 19 Mar 2006 03:22:39 +0100, Martin Jørgensen
<un*********@sp am.jay.net> wrote:
Barry Schwarz wrote:
On Sat, 18 Mar 2006 16:04:24 +0100, "Martin Joergensen"
<un*********@sp am.jay.net> wrote:-snip-
Sorry, your formatting makes it impossible to compile your code and I
didn't spot these in my review.


Okay, forget about this wrapping thing. Guess you're right. I just can't
restrict Visual studio from making lines longer than 65 characters or


It's not a Visual Studio thing. It's a matter of personal discipline.
so. I went through all the options and help. The following should be
better in that I don't think it inserts ekstra linebreaks in places
never intended to have linebreaks. So what's important is what's inside
main() and I hope that copy/pasting should work - I manually fixed some
comments etc.
I compiled your code. I received only two types of errors:

count is unsigned but you pass its address to getdata which is
expecting an int*. Change the prototype and definition.

You define T and T_new twice within the same block, once at
the top of the function and once when you are ready to call malloc.
The latter should be replaced with a simple assignment statement.

After these changes your code compiled fine. Now you get to test.

snip
void getdata(char *filename, unsigned *pCols, unsigned *pRows, int *count); snipint main(void)
{ snip unsigned count, n_x[max_input_files], n_y[max_input_files];
/* and a lot more var's, not necessary in this example */
double **T, **T_new; snip
double **T = malloc((n_y[0]+1)*sizeof(doub le*)); /* n_y = rows */
double **T_new = malloc((n_y[0]+1)*sizeof(doub le*)); /* n_y = rows */ snip}

delete
Remove del for email
Mar 19 '06 #52
Barry Schwarz wrote:
On Sun, 19 Mar 2006 03:22:39 +0100, Martin Jørgensen
<un*********@sp am.jay.net> wrote: -snip-
I compiled your code. I received only two types of errors:

count is unsigned but you pass its address to getdata which is
expecting an int*. Change the prototype and definition.
Yes, stupid mistake - I think I would have found that myself, if I could
just figure out how to make the program compile at that time.
You define T and T_new twice within the same block, once at
the top of the function and once when you are ready to call malloc.
The latter should be replaced with a simple assignment statement.
Yes, I can see that now... I never thought about it before but now it's
ofcourse very clear. This was actually the biggest problem - figuring
out that I should just remove the definition in the line where malloc
was called...
After these changes your code compiled fine. Now you get to test.


Yes, Pedro already mailed it to me and wrote what caused the problems to
this group... So I just needed a small push in the right direction and
that was all. Thanks for helping... Looks like I have everything under
control finally...

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 20 '06 #53
CBFalconer wrote:
Martin Jørgensen wrote:

... snip ...
I guess I have subscribed to this group for about 1,5 months and
being called "a moron", being told to "fuck off" and being told
that "You are quite possibly the most worthless programmer (and
human being) I've ever seen" is just too childish.

You didn't get that from me, and usenet is full of ignorant boors.
Ignore them, or at least their boorish behaviour.


Yeah, you're right. But you know how it is...... I've been on usenet for
a couple of years and have been using it practically every day. It still
pisses you off when you try to behave nice to somebody and that person
repays your gratitude with loads of bullshit, like I've seldom seen.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 20 '06 #54
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-19, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
On Sun, 19 Mar 2006 07:18:56 -0500, in comp.lang.c , CBFalconer
<cb********@yah oo.com> wrote:
void getdata(char *filename, /* room to describe */
unsigned *pCols, /* the various parameters */
unsigned *pRows, /* if desired */
int *count)


I work for a company that has the latter as required style.
(If you violate it you get scorched in code reviews.)


Required always, or required for non-self-explanatory argument lists
and/or argument lists over a certain length?


It's always required. Some old code doesn't follow the coding
standard, but new code must.

The argument comments are stylized: each one must start with IN,
OUT, or IN/OUT according to its usage.
In other words, are we talking about

void f(void /* room to describe void */
)


There'd be no comment on (void) because that's not an argument;
it's a marker that indicates the absence of arguments.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Mar 20 '06 #55
Martin Jørgensen <un*********@sp am.jay.net> writes:
[...]
I guess I have subscribed to this group for about 1,5 months and being
called "a moron", being told to "fuck off" and being told that "You
are quite possibly the most worthless programmer (and human being)
I've ever seen" is just too childish.


Yes, it is. Most of us try not to be such jerks, though.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #56
Martin Jørgensen <un*********@sp am.jay.net> writes:
Mark F. Haigh wrote:
Martin Jørgensen wrote:

-snip-
It may "work", but it's certainly a childish effort, at best. Let me
guess, you're a first year student? Your code sucks. Unclear,
unmaintainable, unportable. Amateur.


No, I'm not a first a first year student.
<snip>
It is not a homework assignment.

<snip>
It is not a homework assignment.

Very cute. I'm impressed. I think all of us are.


I see you have a very big problem with me telling you that this wasn't
a homework assignment. You don't have to be impressed by that. That is
nothing.


Not to defend Mark's rudeness, but it might be nice if you told us
just why you're trying to write this program. Is it part of a larger
project? Is it just a personal exercise? A number of people have
spent considerable time helping you with this; knowing what the goal
is might be helpful. (Sorry if you've already explained this; I
haven't followed the thread very closely.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 20 '06 #57
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-19, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:

On Sun, 19 Mar 2006 07:18:56 -0500, in comp.lang.c , CBFalconer
<cb********@yah oo.com> wrote:
>void getdata(char *filename, /* room to describe */
> unsigned *pCols, /* the various parameters */
> unsigned *pRows, /* if desired */
> int *count)

I work for a company that has the latter as required style.
(If you violate it you get scorched in code reviews.)


Required always, or required for non-self-explanatory argument lists
and/or argument lists over a certain length?


It's always required. Some old code doesn't follow the coding
standard, but new code must.

The argument comments are stylized: each one must start with IN,
OUT, or IN/OUT according to its usage.


What exactly is an "OUT" argument? a pointer? what would you call a FILE
* argument that is supposed to be read to / written from?
Mar 20 '06 #58
Jordan Abel <ra*******@gmai l.com> writes:
On 2006-03-20, Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
The argument comments are stylized: each one must start with IN,
OUT, or IN/OUT according to its usage.


What exactly is an "OUT" argument? a pointer?


A pointer that is written to.
A pointer that is only read from would be an IN argument (and
would generally be marked `const'.)
what would you call a FILE * argument that is supposed to be
read to / written from?


It's debatable. Generally this sort of thing would be clarified
elsewhere. Comments are (generally) meant for consumption by
humans, not computers, so you don't need rules for them that can
be evaluated by computers.
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
Mar 20 '06 #59
Keith Thompson wrote:
-snip-
Not to defend Mark's rudeness, but it might be nice if you told us
just why you're trying to write this program. Is it part of a larger
project? Is it just a personal exercise? A number of people have
spent considerable time helping you with this; knowing what the goal
is might be helpful. (Sorry if you've already explained this; I
haven't followed the thread very closely.)


I think this is off-topic, at least I consider the on-topic discussion
finished.

However, since you absolutely must ask: I'm trying to learn and figure
out how to do basic things in C since I'll graduate as an engineer this
summer (I'm on eigth semester, not a first first year student as "Mark
the child" suggested). Since I'm writing my bachelor project now, I have
plenty of time to learn, to do and make my own exercises or do whatever
I want to learn, (including doing nothing). Usually Matlab is the
preferred choice here but I think C is important to learn, especially
for doing loops. You don't have a problem with me trying to learn C, now
do you?

Because if not, then everything is fine even though this is off-topic.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Mar 21 '06 #60

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

Similar topics

1
1596
by: Dejavous | last post by:
Hi there, I'm a SQL Server 2000 noob and looking for some help. I need to make a simple script that counts the number of rows in a table. Also, i need to make another version ofthe same script, but ONLY look and count rows with certain data in them...e.g. search and count for rows with the word JOE in them. Any help would be really appreciated, and if somebody would be so kind to break down and explain each part of the script i would be...
1
1471
by: dukefrancis | last post by:
Can anyone tell me how to find the count of columns from a table????
3
1554
by: sheesh | last post by:
hi how am i able to input rows of integers from another file for eg num.txt inside the text file it is as shown 1 3 5 2 4 6 7 9 11 i tried doing so but still couldn't get it string s;
1
1340
by: neehakale | last post by:
If anybody of you knows how to count the no of rows in the excel file then pls tel me....
0
3005
by: JFKJr | last post by:
I have an excel file, which has columns C and D grouped together, I am trying to delete blank columns and rows from the excel file, ungroup the columns and import the file to MS Access using Access VBA code. The following is the Access VBA code I used to delete blank columns and rows in the excel file. But, unfortunately, the resultant excel file still has two columns (C and D) grouped together, so when I am importing the file to MS Access,...
1
1913
by: mojo123 | last post by:
Hi All, I am looking for a way to count rows in datagrid. The code I have goes something like this: Data1.DatabaseName = Mydata.mdb Data1.Recordsoource = Select * Table1 Where Class = 3 And Group=2 Data1.Refresh How can I count the number of current rows/record in a datagrid. Any Help will be greatl appreciated.
6
2928
by: viki1967 | last post by:
Hi all. I need your help. I realize this script and I do not know where to begin: 1) A simple grid rows / columns. 2) The first column contain an input type = "checkbox" 3) When select the input type, the line should change color than the other rows, and you should open a popup window.
3
7051
by: ronakinuk | last post by:
how can i unhide rows/columns in excel 2007
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10836
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7960
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5793
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4210
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.