Infile help would be nice

need help on getting input from a text file

Page 1 of 1

6 Replies - 1800 Views - Last Post: 16 May 2001 - 09:47 PM Rate Topic: -----

#1 EnKRYpTeD  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 11-May 01

Infile help would be nice

Posted 15 May 2001 - 09:25 PM

Ok, i want to know how to get input from a text file to the program or how it works. Here is what i have and its not working as well as i thought my wonderful code would....


[code]
.
.
.
infile.open("infile.txt"ios::in)// [color=green]right after this i need it to input 6 numbers now what exactly do i use out of the following..
infile>> a;
infile>> b;(and so on)
OR
infile.get(a);
infile.get(b);
infile.get©; and so on...  now.. are these close or      totally wrong?

here is how im trying to use it

{
cout<<"Would you like to play the disc game?";
cin.get (ans,4);

if(strcmp(ans, "yes" )==0)
beef:
{cout<<"Would you like to input from a file? y for yes n for no
";
cin>>ansa;
switch(ansa)
{
case 'y':
infile.open("Finalinput.txt", ios::in);
cout<<"infile opend
";
info();
break;
case 'n':
info();
break;
default:
cout<<"Please enter the correct answer
";
goto beef;
break;
}
}
else if(strcmp(ans,"no" )==0)
cout<<"thank you for visiting....."<<endl;  
else
cout<<"Please follow directions!
";
}
void info()
{

cout<<"Please type Coefficients and constants a, b, c, d, e, f!";
cin>>a
  >>b//i need it to input a number for each variabl
  >>c
      >>d
      >>e
  >>f;

calculate(a, b, c, d, e, f);
}

im looking at a book right now that shows both methods but neither seem to work. If you can help, DONT GIVE ME ANSWERS JUST ADVICE OR GUIDENCE PLEASE! thank you, i will appreciate it a lot thanks! or, if you need more info tell me thanks!

(Edited by EnKRYpTeD at 11:25 pm on May 15, 2001)


Is This A Good Question/Topic? 0
  • +

Replies To: Infile help would be nice

#2 kalpeshmjoshi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 25-April 01

Re: Infile help would be nice

Posted 16 May 2001 - 02:29 AM

yes you are very near by using infile.get()
now the what u thing for that is how you have stored the number in textfile is it in ascii formate mean if your store a value 123 in file in ascii format then it will store as 123 of 3 cherecter but in binary it will store as a single charecter representing value of 123.  If you still dont under stand of cant find the way then tell me about how you store the value in the file (Format of your File) i will guide you
[color=blue]
Was This Post Helpful? 0
  • +
  • -

#3 Null and Void  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: Infile help would be nice

Posted 16 May 2001 - 05:27 AM

This is how I'd get 6 numbers of a text file (seperated by spaces, tabs, or newlines) into an array, for example (using Standard ANSI C i/o).
int num[6];

FILE *fp = fopen("SomeFile.txt","rt"); if(fp!=NULL) {  fscanf(fp,"%d%d%d%d%d%d",&num[0],&num[1],&num[2],&num[3],&num[4],&num[5]);  fclose(fp); }


Was This Post Helpful? 0
  • +
  • -

#4 kalpeshmjoshi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 25-April 01

Re: Infile help would be nice

Posted 16 May 2001 - 06:33 AM

Hi Null and Void its Ok for this thread and code are in C also, becouse  EnKRYpTeD has tried to solve his problem but in any other thread dont post the Code directly, give the hints, tute him this is our new policy in this site.

Its ok for perticular this thread but remember for other thread
[color=Blue]

Was This Post Helpful? 0
  • +
  • -

#5 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4517
  • View blog
  • Posts: 28,417
  • Joined: 21-March 01

Re: Infile help would be nice

Posted 16 May 2001 - 08:34 PM

this is what i got


void openinfile()
{infile.open ("Celsius-Farenheit Conversion infile.txt", ios::out);//opens infile
for(n=j=1;n<8;n++&&j++)
{
cout << "Please enter the value for day #"<<j<<":";
infile.getline(temp[i][n], sizeof(temp[i][n]));
}
system("cls");//clears screen
}

the only problem is that i am getting this error:

error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'long' to 'char *'

dont know how to fix it while using infile, any suggestion? o, and remember, this code is formatted for my program, so just ignore the for loop

Was This Post Helpful? 0
  • +
  • -

#6 EnKRYpTeD  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 11-May 01

Re: Infile help would be nice

Posted 16 May 2001 - 08:36 PM

[color=red]OMG i am soooo confused on the infile! ok, when i start my program does it automatically import the numbers or do i have to do something. Also, do i start the word pad or winwod before i run the program?

Ok, heres a little code on my newst idea of how this works

when i open the infile right here...
infile.open("Input", ios::in) <<this is right? right?

i need to import the 6 numbers for a b c d e and f... like down here
cin>>a, b, c, d, e, f;
now... do i do this?
infile>>a, b, c, d, e, f;
or...
cin.get>>a, b, c, d, e, f;

Now, on the text file how do i put the numbers down that i want to input. like this? 0 4 0 0 0 -9 or... 0, 4, 0, 0, 0, -9 or what?

ive been told that cin.get works, but when i use it it doesnt... here is what i have for the input part...

void info()
{
 infile.open("input.txt", ios::in)
  cout<<"Please enter a number for each coefficient a-f
";
  cin>>a>>b>>c>>d>>e>>f;// for some reason when i use     commas it doesnt work here...

now would i do cin.get ("a")? or cin.get "a"  just for the first coefficiant.

Now, i have also heard you can do both a input and output file in one shot, how exactly would you arrange the code for it to work. Something like this?
infil.open..blah blah
outfile.open bla blah
infile.close
outfile.close or yea, if anyone can help me please it would be nice, i would like to sleep tonight thanks ;)


Was This Post Helpful? 0
  • +
  • -

#7 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Infile help would be nice

Posted 16 May 2001 - 09:47 PM

first off you shouldn't be getting the input from cin you should just be getting it from the text file by actually creating a text file called input. secondly you have to put the file extension on the file "input.txt".you also need to get it from the user. i would just run the program using the text file. then i would run through the user input. first you have to make sure that there is stuff in the file. what exactly is your problem any way.

also you need to save ALL of your output using ofstream.

Supersloth getline is iso ansi standard i think which you don't know. either that or you just need string.h try using cin.get(variable, size).

Umm i don't really know what else could be wrong

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1