473,326 Members | 2,023 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,326 software developers and data experts.

Can't read entire record - fscanf (choked up)

....
somewhere, a newbie is dying ...
Is there anybody out there who can help me get the right input
for the following segment? I am trying to read entire records
of 5 (2-digit) integers at a time. It would be best to read the
integers into their own 5 respective variables.
I thought I had it. I've redone my program so many different ways
I am coming to the same conclusion I did when I tried to do it
several years ago: that it cannot reasonably be coded in any popular
language. I just now gave up on reading each individual integer
because I was getting output that looked like my
fscanf(fileptr, "%i", &n); statement was reading entire records instead.
My first record is: 17 40 41 42 46
The following code seems to go only once thru the first 2 FOR loops
(on step-through) and bombs about 46 times thru on the 3rd FOR loop,
with a:
"unhandled exception -- access violation" error.
Using MVC/C++ 4.2 -- is this too "old."
Please HELP (WTH IS GOING ON???)

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <math.h>

int main()
{ /* 52 rows x 120 active columns */
int current[53][122] = {0}; /* include extra R & C for testing */
int n1, n2, n3, n4, n5;
int row=0, col=1, cycle=0;
FILE *fileptr;
if ((fileptr = fopen("test_data.txt", "r")) == NULL )
....error checking ...

for (cycle = 0; cycle < 120; cycle++) /* 5 entries x 120 data recs */
{ /* was 1 to 601 cycles */
fscanf(fileptr, "%i", "%i", "%i", "%i", "%i", &n1, &n2, &n3, &n4, &n5);

printf("%i", "%i", "%i", "%i", "%i", n1, n2, n3, n4, n5);
for (row = 0; row < (n1 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n1 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n1 +1); row < (n2 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n2 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n2 + 1); row < (n3 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n3 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n3 + 1); row < (n4 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n4 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n4 + 1); row < 52; row++)
{
current[row][col] = current[row][col - 1];
if (n5 == row) {
current[row][col] = current[row][col] + 1;
}
}
col++;
}
Nov 13 '05 #1
2 2800
First of all, I want to get the one thing about my development
progress so far.
( Eric Sosman last wrote: )
... perhaps the problem lies not with the language, but with the
person unable to use the language; it's a poor workman who blames
I already KNOW this, but if I am going to move on to spend my time on
something of use to myself, I am not going to start out by kicking
myself. And teaching oneself a programming language is unlikely, for
one thing, and not very rewarding for an able-bodied adult. By
itself, I can get better income support from taking on a night job
(janitor, road mantenance, etc.) instead of challenging myself to
expand my horizons.
I just hate to admit defeat without at least pulling out a few
"cheats."

Anyway, I this board has a lot of interesting tips and tricks, for the
occasional solution to a given problem. Eric Sosman's comments here
are really dead-on....

for (cycle = 0; cycle < 120; cycle++) /* 5 entries x 120 data recs */
{ /* was 1 to 601 cycles */
fscanf(fileptr, "%i", "%i", "%i", "%i", "%i", &n1, &n2, &n3, &n4, &n5);


This is completely wrong. fscanf() takes *one* format string

.... What you probably wanted was

fscanf(fileptr, "%i%i%i%i%i", &n1, &n2, &n3, &n4, &n5);
This is much better, and seems to work except for the occasional
skipover (n5 on the 7th record, for example.)

... but even that isn't very good. fscanf() can fail -- it can....

I checked simple datafile and all records are identical formatting. I
think I will poll a local professor about my logic - show him my
inputs, listing and outputs. I think the program control makes
sense. The only improvement I've been able to add from Eric's code
is that the %i%i... convertors work better as explicitly 2-digit
specifiers: %2i%2i...
I had a ten (10) being read as a 1 - but not 20, 30, 40, or 50!!!
You (eric) mentioned this, I think:

Assuming you've successfully read and converted some numbers,
putting blind faith in them is a poor idea. What if the input
accidentally contains "4041" instead of the intended "40 41"?

....

Thanks for the help Eric. I really needed something that looked like
progress cuz I was about to opt for the "indefinite time-out."

Any chance I can get a little more help on one more idea?
Is there a function, in ANSI/ISO basic C, that would "normally" be
used to read a numerical data file (with numerous records of identical
composition) record-by-record into a numerical buffer array (of size
appropriate to 1 record of data), WITHOUT using a structure??? Seems
like this would be a common need in programming, but maybe C requires
use of structures or other advanced I/O techniques for this.
Off-subject item....
I suspect this beginner stuff has been asked by someonbe else in the
past year or so. I really wish google had a special search engine for
newsgroup postings. Technical subjects would benefit from this,
informal ones (alt.binaries.misc.perversion) couldn't, it seems.
Google - you listenin??? You need a form for posters to fill out
with a half-dozen interactive fields to construct a header, above &
before the nearly useless "Thread Subject" title. Interactive like
eBay's listings are offered to sellers. Maybe a little more
ownership in this medium would elevate it above the aimless
exhibitionism it is. I could settle most of my novice problems
through other's mistakes faster than I can pester experts enough that
they will break down and respond directly to pleas for help on a
specific matter.
Nov 13 '05 #2
Blankdraw wrote:

[...] And teaching oneself a programming language is unlikely, for
one thing, and not very rewarding for an able-bodied adult. [...]
"Unlikely?" Why? My own personal experience is exactly
the opposite: I've learned fifteen-plus programming languages
almost entirely through self-study. I've taken classes in
programming languages only twice, and neither was as successful
a means of learning as was studying the language on my own.

Different strokes for different folks, I guess. Anyhow ...
Any chance I can get a little more help on one more idea?
Is there a function, in ANSI/ISO basic C, that would "normally" be
used to read a numerical data file (with numerous records of identical
composition) record-by-record into a numerical buffer array (of size
appropriate to 1 record of data), WITHOUT using a structure??? Seems
like this would be a common need in programming, but maybe C requires
use of structures or other advanced I/O techniques for this.


The approach I'd recommend would be to read lines of text
with fgets(), and then use functions like strtol() to extract
the numbers. The reason for doing it this way is that if
something goes wrong -- "1O" instead of "10" in the input,
for example -- you can do a much better job of detecting and
repairing or reporting the problem than you can with fscanf().
Or again: if a line somewhere in the middle of the file has
six numbers or four numbers instead of five, fscanf() won't
consider it an error but will simply get "out of step" with
the line breaks; line-at-a-time-and-pick-it-apart lets you
catch this problem as soon as it crops up.

I'm not sure why you bring up structures in this context.
You seem to want to process your numbers in batches of five,
and there doesn't seem to be a lot of "difference in kind"
between the numbers in each batch. The natural aggregate for
this would appear to be an array of five values, not a struct.

--
Er*********@sun.com
Nov 13 '05 #3

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

Similar topics

7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
11
by: nick | last post by:
the following code is used to read a file called "input.txt" # include <stdio.h> int main(int argc,char *argv){ FILE *fp; int ch; fp=fopen("input.txt","r"); ch=fscanf(fp,"%d",&ch);...
18
by: strchild | last post by:
Hey Guys, I've been perusing the newsgroup here for some time now, and going through my ever so non-helpful book, Microsoft Visual Basic .NET Step by Step Version 2003, over and over, and I feel...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
16
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.