freelanceprogrammers.org Forum Index » C
file input
Joined: 20 Jul 2003
Posts: 6
file input
Hi all,
I have problems with reading in from a file.
The first line and the first integer (variable minute) is read in correctly,
but there`s problems with the rest of the file. The file format is fixed and
cannot be altered.
The input file is as follows:
1 10 1 10 1 10 1 10 0 0 1 10
1 a 40 b 24 c 35 d 6 e 6 f 6 g 6
h 6 i 6 j 6 k 6 // a to k is stored as
name[1][0..11], and item[1][0..11]
2 A 4 B 4 C 4 D 4 E 4 F 4 G 4
H 4 I 4 J 4 K 4 L 4 N 4 O 4
P 4 Q 4 R 4 S 4 T 4 // A to T is stored as
name[2][0..19], and item[1][0..19]
3 U 1 // U is stored as name[3][0] and item[3][0]
26 V 1 // V is stored as name[4][0] and item[4][0]
// 3 newlines till the end-of-file
All the alphabets are stored as "name" and the integer behind each alphabet
is stored as "item" for the corresponding "name". Both variables "name" and
"item" are stored as 2D array, i.e. name[minute][i].
My code is as follows:
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
void readmin(int& minute, int nextmin, ifstream& infile, int i)
{
char name[30][20];
int item[30][50];
char temp;
while (nextmin == 1)
{
infile >> name[minute][i] >> item[minute][i];
i++;
if (infile.get() == `
`)
{
infile >> temp;
if (isdigit(temp)) // check if the input is another minute
{
nextmin = 0;
minute = temp; // Problem: Is there a way to change char to
int?
}
else
{
name[minute][i] = temp;
infile >> item[minute][i];
i++;
}
}
}
}
void main()
{
ifstream infile;
infile.open("ffs2.txt", ios::in);
int nextmin;
int i, minute = 0, op[6], e[6];
for (i = 0 ; i < 6 ; i++)
{
op[i] = 0;
e[i] = 0;
}
for (i = 0 ; i < 6 ; i++) // To read in the first line of 1s, 0s, and
10s
{
infile >> op[i] >> e[i];
}
infile >> minute; // read in the first minute
while (!infile.eof())
{
nextmin = 1;
i = 0;
readmin(minute, nextmin, infile, i);
}
infile.close();
}
I`m trying to call the function "readmin()" to read in the "minute", "name"
and "item" from "a 40" (in the 2nd line of input file) until the end of
file. I`m using Borland 5.0. When I run the program, it tells me "Thread
stopped.... Access Violation....".
Please tell me what`s wrong with my program.
Thank you.
Regards,
Rayne
Joined: 19 Jul 2003
Posts: 29
file input
Hi Rayne,
Could you re-post the files (especially the data file)
as mail-attachment? Yahoo doesn`t show them correctly
(I have HTML-mode turned off), so I can`t figure out
how should it be read.
Regards,
Shantanu
--- Rayne <ghcgwgwg@...> wrote:
> Hi all,
> I have problems with reading in from a file.
> The first line and the first integer (variable
> minute) is read in correctly,
> but there`s problems with the rest of the file. The
> file format is fixed and
> cannot be altered.
>
> The input file is as follows:
>
> 1 10 1 10 1 10 1 10 0 0 1 10
>
> 1 a 40 b 24 c 35 d 6 e 6 f 6
> g 6
> h 6 i 6 j 6 k 6 // a
> to k is stored as
> name[1][0..11], and item[1][0..11]
> 2 A 4 B 4 C 4 D 4 E 4 F 4
> G 4
> H 4 I 4 J 4 K 4 L 4 N 4
> O 4
> P 4 Q 4 R 4 S 4 T 4 // A
> to T is stored as
> name[2][0..19], and item[1][0..19]
> 3 U 1 // U is stored as name[3][0] and
> item[3][0]
> 26 V 1 // V is stored as name[4][0] and
> item[4][0]
> // 3 newlines till the end-of-file
>
>
>
> All the alphabets are stored as "name" and the
> integer behind each alphabet
> is stored as "item" for the corresponding "name".
> Both variables "name" and
> "item" are stored as 2D array, i.e. name[minute][i].
>
> My code is as follows:
>
> #include <iostream.h>
> #include <fstream.h>
> #include <ctype.h>
>
> void readmin(int& minute, int nextmin, ifstream&
> infile, int i)
> {
> char name[30][20];
> int item[30][50];
> char temp;
> while (nextmin == 1)
> {
> infile >> name[minute][i] >> item[minute][i];
> i++;
> if (infile.get() == `
`)
> {
> infile >> temp;
> if (isdigit(temp)) // check if the input
> is another minute
> {
> nextmin = 0;
> minute = temp; // Problem: Is there a
> way to change char to
> int?
> }
> else
> {
> name[minute][i] = temp;
> infile >> item[minute][i];
> i++;
> }
> }
> }
> }
> void main()
> {
> ifstream infile;
> infile.open("ffs2.txt", ios::in);
> int nextmin;
> int i, minute = 0, op[6], e[6];
> for (i = 0 ; i < 6 ; i++)
> {
> op[i] = 0;
> e[i] = 0;
> }
> for (i = 0 ; i < 6 ; i++) // To read in the
> first line of 1s, 0s, and
> 10s
> {
> infile >> op[i] >> e[i];
> }
> infile >> minute; // read in the first minute
> while (!infile.eof())
> {
> nextmin = 1;
> i = 0;
> readmin(minute, nextmin, infile, i);
> }
> infile.close();
> }
>
> I`m trying to call the function "readmin()" to read
> in the "minute", "name"
> and "item" from "a 40" (in the 2nd line of input
> file) until the end of
> file. I`m using Borland 5.0. When I run the program,
> it tells me "Thread
> stopped.... Access Violation....".
> Please tell me what`s wrong with my program.
>
> Thank you.
>
> Regards,
> Rayne
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
>
> To unsubscribe from this group, send an email to:
> Programmers-Town-unsubscribe@yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
Joined: 20 Jul 2003
Posts: 6
file input
Hi all,
I have problems with reading in from a file.
The first line and the first integer (variable minute) is read in correctly, but there`s problems with the rest of the file. The file format is fixed and cannot be altered. I`m sending the file input and the cpp file as attachments.
All the alphabets are stored as "name" and the integer behind each alphabet is stored as "item" for the corresponding "name". Both variables "name" and "item" are stored as 2D array, i.e. name[minute][i].
From the input file,
a 40 to k 6 is stored as name[1][0..11], and item[1][0..11]A 4 to T 4 is stored as name[2][0..19], and item[1][0..19]U 1 is stored as name[3][0] and item[3][0] respectivelyV 1 is stored as name[4][0] and item[4][0] respectively
3 newlines till the end-of-file
I`m trying to call the function "readmin()" to read in the "minute", "name" and "item" from "a 40" (in the 2nd line of input file) until the end of file. I`m using Borland 5.0. When I run the program, it tells me "Thread stopped.... Access Violation....".
Please tell me what`s wrong with my program.
Thank you.
Regards,
Rayne
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







