473,757 Members | 7,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_dat a.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 2829
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.m isc.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
2264
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
11
1441
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); printf("%d\n",ch); fclose(fp);
18
1668
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 like such a dummy. I read you fella's talking up one side and down the other about streams, and can't for the life of me get a good grasp of them. I've been reading the articles you toss around to each other but I can't quite follow them enough...
9
5211
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
4
4232
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 "may" because there is no score. Anyone knows what is the workaround to this problem?
4
12440
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
7
3059
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 2718 2895 I wanna to read the data to an array, as the follows:
16
1785
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 wanted it to. Code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h>
6
418
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 set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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
9737
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...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
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.