473,769 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Input Question

a
(I've reached that familiar place where I've got a nagging little
problem in a program I'm writing but I've been staring at code for too
long and I probably wouldn't be able to recognize the answer even if I
was staring right at it.)

I'm trying to design a function that reads input from my data file into
(a pair of) arrays. Simple enough? However, each line of the file is
either going to be 4 integers or a character (then a carriage return)
and I'm not sure which (i.e. there could be 4 integers followed by
another line of 4 integers followed by a letter or it could just be 1
letter/1 letter/1 letter). If the input is a character, it goes into
charArray, but if the input are those 4 integers, then they take up the
next 4 spots in intArray.

So, basically, I need to know how to read in something from a file when
I don't know its type. Also, I'm a bit rusty on the C++ and I've never
done file I/O so I'm trying to do this in the simplest way possible but
most of the file input examples I've seen online contain many function
calls that I don't understand. So when explaining, please do the typing
equivalent of speaking loudly and slowly =)

//The initializations are simple enough:
int in1, in2, in3, in4;
char ch1;

ifstream file_in;
file_in.open("t est.txt");

if (!file_in)
{
cout << "Cannot open the file test.txt" << endl;
return (-1);
}

//And I think I have the while loop:
while( (file_in >> in1 >> in2 >> in3 >> in4 ) || (file_in >> ch1) )

//But then, I'm not sure how to check for type with my input
thanks in advance,
-frank (email: ro******@NOSPAM .yahoo.com)

Jul 19 '05 #1
6 2921
a wrote:
(I've reached that familiar place where I've got a nagging little
problem in a program I'm writing but I've been staring at code for too
long and I probably wouldn't be able to recognize the answer even if I
was staring right at it.)

I'm trying to design a function that reads input from my data file
into (a pair of) arrays. Simple enough? However, each line of the
file is either going to be 4 integers or a character (then a carriage
return) and I'm not sure which (i.e. there could be 4 integers
followed by another line of 4 integers followed by a letter or it
could just be 1 letter/1 letter/1 letter). If the input is a
character, it goes into charArray, but if the input are those 4
integers, then they take up the next 4 spots in intArray.

[SNIP]

Since you input is line based, you will need to use the getline
functionality. Get the line into an std::string and when it is there, you
can find out if it is a 4*int or a 1*char. Be careful that a 1*char looks
just like a 1*int.

The iostream extract operator makes no difference between the any whitespace
and the end of line, so your approach with the two reads hoping that the int
reader will fail on a char is wrong. If the char happens to be 1, it will
read it as the first integer. If there are 4 lines like this you will get 4
intergers and so forth.

If you did read a line into a string you can easily check if it has one
character in it or not (by looking at the size). If not put this string
into an istringstream and get the 4 integers out.

--
Attila aka WW
Jul 19 '05 #2
a
> Since you input is line based, you will need to use the getline
functionality. Get the line into an std::string and when it is there, you
can find out if it is a 4*int or a 1*char. Be careful that a 1*char looks
just like a 1*int.

The iostream extract operator makes no difference between the any whitespace
and the end of line, so your approach with the two reads hoping that the int
reader will fail on a char is wrong. If the char happens to be 1, it will
read it as the first integer. If there are 4 lines like this you will get 4
intergers and so forth.

If you did read a line into a string you can easily check if it has one
character in it or not (by looking at the size). If not put this string
into an istringstream and get the 4 integers out.


Oops, I have a small problem. I reread what I wrote and I said exactly
4 but it's actually anywhere from 1 to 4 integers on a line.
Unfortunately, as you mentioned in the first paragraph, this creates a
problem when I'm just trying to search on length alone. Is there any
way to test for data type of the input rather than length of it?

-frank

-frank

Jul 19 '05 #3
a wrote:
Since you input is line based, you will need to use the getline
functionality. Get the line into an std::string and when it is
there, you can find out if it is a 4*int or a 1*char. Be careful
that a 1*char looks just like a 1*int.

The iostream extract operator makes no difference between the any
whitespace and the end of line, so your approach with the two reads
hoping that the int reader will fail on a char is wrong. If the
char happens to be 1, it will read it as the first integer. If
there are 4 lines like this you will get 4 intergers and so forth.

If you did read a line into a string you can easily check if it has
one character in it or not (by looking at the size). If not put
this string into an istringstream and get the 4 integers out.


Oops, I have a small problem. I reread what I wrote and I said
exactly 4 but it's actually anywhere from 1 to 4 integers on a line.
Unfortunately, as you mentioned in the first paragraph, this creates a
problem when I'm just trying to search on length alone. Is there any
way to test for data type of the input rather than length of it?


Frank. Honestly. Who made that specification? Please let me know:

1
1
1

Was this 3 lines of one characters or 3 lines of one integer?

--
Attila aka WW
Jul 19 '05 #4
a
> Frank. Honestly. Who made that specification? Please let me know:

1
1
1

Was this 3 lines of one characters or 3 lines of one integer?


As for who made that design spec- it's poor data structure design. I'm
in the process of refining it. Obviously it still has some work to go.
*sheepish grin*

Basically, the concept is that if a player (each designated by a letter)
plays a variable number of games per week, hence the different number
of, well, numbers. Basically, in the example below, player A (I'll get
names implemented later- I'm just working on getting the smaller issues
right first- but that string is another reason why I want to be able to
determine things via data type) only had x number of games each week (2,
1, 3 in the example below) and the numbers denote the number of points
per game.

A
8 12
1
2 3 5
As for
1
1
1
Those would all count as integers. Single integers are not characters-
sorry for not clarifying.

-frank

Jul 19 '05 #5
a wrote:
[NIP]
Basically, the concept is that if a player (each designated by a
letter) plays a variable number of games per week, hence the
different number of, well, numbers. Basically, in the example below,
player A (I'll get names implemented later- I'm just working on
getting the smaller issues right first- but that string is another
reason why I want to be able to determine things via data type) only
had x number of games each week (2, 1, 3 in the example below) and
the numbers denote the number of points per game.

A
8 12
1
2 3 5


OK. As I said: read lines into an std::string using getline, since your
file is lone based. Then check the first character if it is a letter.
Let's suppose you are only using that English 26, so it should be pretty
simple. If it is a letter, you have it: a new player. If it is not, it is
better be a number, because you put Mr. Std. String into a stringstream and
read integers from it one by one in a loop, until you can.

--
Attila aka WW
Jul 19 '05 #6

"a" <a@a.net> wrote in message news:vn******** ****@corp.super news.com...
Frank. Honestly. Who made that specification? Please let me know:

1
1
1

Was this 3 lines of one characters or 3 lines of one integer?
As for who made that design spec- it's poor data structure design. I'm
in the process of refining it. Obviously it still has some work to go.
*sheepish grin*

Basically, the concept is that if a player (each designated by a letter)
plays a variable number of games per week, hence the different number
of, well, numbers. Basically, in the example below, player A (I'll get
names implemented later- I'm just working on getting the smaller issues
right first- but that string is another reason why I want to be able to
determine things via data type) only had x number of games each week (2,
1, 3 in the example below) and the numbers denote the number of points
per game.

A
8 12
1
2 3 5


Why not change the file format to tell you this information?
E.g.

A 3 // 3 is number of following lines related to 'A'
2 8 12 // 2 is num of scores, 8 and 12 are the two scores
1 1 // 1 is num of scores, 1 is the single score
3 2 3 5 // 3 is num of scores, 2, 3, and 5 are the three scores.
As for
1
1
1
Those would all count as integers. Single integers are not characters-
Actually *everything* in a text file is a character,
including single digit characters such as '1'.
A sequence of digit characters can be converted
to an integer type, but that doesn't stop them
from being characters, i.e. valid as a string.
sorry for not clarifying.


If you say that a single numerical character does not
qualify as a 'character' in your file specification,
then you'll have to disallow such from being a 'player
name'.

I think it's much more effective to create a good design
for your file format, rather than trying to come up with
'fancy tricks' to try to decipher one with a limited amount
of information.

-Mike
Jul 19 '05 #7

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

Similar topics

14
1780
by: Michael R. Copeland | last post by:
I'm writing an application that requires an "intelligent merge" of 2 files. That is, equal data has a "preferred source" that I want to write out. What I have works, I believe, but it seems horribly cumbersome (having to set the input variables to ""...). Is there a better way? TIA while ((!feof(wf3)) || (!feof(wf1))) { if (!feof(wf1)) {
0
3940
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
6
4135
by: tshad | last post by:
I have an upload file input as: <input id="MyFile" style="width:300px" type="File" runat="Server"> This works fine, but I find that if my page doesn't pass validation during postback, the page comes back with all the data intact, except for the upload object. The text box for "MyFile" (my example) is always cleared. Why is that and is there a way to stop that from happening? Thanks,
3
2223
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the same file stream. I was going to pass a ref to the file stream to the second thread, but the problem comes when I am trying to read from one section of the stream and write to another. I dont want one thread changing the position of the stream...
12
2989
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this and not have it affected by the null? because it is right now causes truncated data at wierd places... but as soon as i manually with a hex editor change char(00) to char(20) in the files it reads prerfectly... which leads me to my 2nd...
4
4487
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use one of my webform pages which includes a button that pops up a window where you can upload files, if you upload files in this popup window, it seems to somehow clear out all of the session variables and the users get logged out. However, if...
15
14435
by: Matt | last post by:
Is there a way to display the file selection window for a file input field via JavaScript? My goal is to emulate the behavior seen in Yahoo! Mail BETA. When adding an attachment, it displays a file selection window when you click a custom button/icon. I saw an example of using CSS to actually position the browse button behind an image. I am hoping that isn't the only option.
10
2067
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach is like this . main() { char buf; FILE *fp;
6
2488
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed from user to calculate the score. Here is a problem. I can read a text file. However, it's read whole file at a time. So,
14
12828
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in case it's a question, i want to popup a messageBox or something, and bring back to the batch file the result (Yes\No question). I know how to excute the batch file and get all the Standard output at the end, but i don't know who can i read it line by...
0
9587
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...
0
9423
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
10045
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
9993
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
9863
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
8870
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
3
2815
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.