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

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("test.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 2900
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.supernews.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
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...
0
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....
6
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...
3
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...
12
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...
4
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...
15
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...
10
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...
6
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...
14
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.