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

need to read multiple inputs.

Okay, so I can get this to read the first line of a txt document. I just can't figure out how to get it to read the next lines. I have to use "user_input" in a function. The wait function just waits until the user hits enter. The text document has 11 lines of code. I know this is a total newb question, but I am baffled. The function gets called at the begining of the file.
Expand|Select|Wrap|Line Numbers
  1. int file_run(void){
  2.     char buffer[255];
  3.     ifstream command_input_file;
  4.     command_input_file.open("p3test.txt", ios::in);
  5.     command_input_file.getline(buffer, 255);
  6.     if(command_input_file == NULL){ return 0;};
  7.     strcpy(user_input,buffer);
  8.     wait();
  9.     return 0;
  10.  
HERE IS A PART OF MY CODE -SMALL PART:

Expand|Select|Wrap|Line Numbers
  1. int parse_command_line(void)
  2. {                if(COMMANDS_FROM_FILE == 1){
  3.                 file_run();}
  4.  
  5.                 if(COMMANDS_FROM_FILE ==0){
  6.                 cout << "Enter the Commmand: \n";
  7.                 cin.getline(user_input,' ');
  8.                 }
  9.                 // deliminates spaces and uses to separate tokens
  10.                 character_string = strtok(user_input," ");
  11.                 //compares the length of the string to allowable amount of characters.
  12.                 if( (strlen(user_input)) >= MAX_CMD_LINE_LENGTH){
  13.                         cout << "Please Use " << MAX_CMD_LINE_LENGTH << " Characters or Less.\n";
  14.                         return 0;}
  15.                 //cout << "Your Tokens Are: \n \n";
  16.                 j = 0;
  17.                 //This continues to process each character in the string until it reaches the end.
  18.                 while(character_string != NULL)
  19.                 { //Analyzises each character in the string, one at a time.
  20.                     //character[j] = character_string;
  21.                     strcpy(cStrings[j],character_string);
  22.                     //error case if there are more than the 10 allowable tokens.
  23.                     if(j > (MAX_TOKENS_ON_A_LINE)-1){
  24.                         cout << "Please Enter " << MAX_TOKENS_ON_A_LINE << " Tokens or Less.\n";
  25.                          return 0;
  26.                     }
  27.                     //cout << character[j] << endl;
  28.                     j = j + 1;
  29.                     character_string = strtok(NULL, " ");
  30.                 }  
  31.  
  32. if
  33.  
Feb 6 '07 #1
2 1659
horace1
1,510 Expert 1GB
you could have two seperate functions, one which opens the file and another which reads lines of text, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <fstream.h>
  2. #include<iomanip.h>
  3. ifstream command_input_file;
  4. void file_open()
  5. {
  6.     command_input_file.open("p3test.txt", ios::in);
  7.     if(! command_input_file)
  8.       {
  9.           cout << " error opeing file " << endl;
  10.           exit(-1);
  11.          }
  12. }
  13.  
  14. int file_run(void){
  15.     char buffer[255];
  16.     command_input_file.getline(buffer, 255);
  17.     if(command_input_file == NULL){ return 0;};
  18.     strcpy(user_input,buffer);
  19.     wait();
  20.     return 1;  // return 1 for OK 0 for fail
  21. }
  22.  
you keep calling file_run() until it returns 0
Feb 6 '07 #2
Thank You very much it was very helpful!

Doing this fixed it all:

Expand|Select|Wrap|Line Numbers
  1.     if(COMMANDS_FROM_FILE == 1){
  2.                     if(k == 1){ 
  3.                         file_open();
  4.                         k = k-1;
  5.                     }
  6.  
  7.                 file_run();}
  8.  
  9.     if(COMMANDS_FROM_FILE ==0){
  10.             cout << "Enter the Commmand: \n";
I had been staring at this for a couple hours yet.....
Feb 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
1
by: Avin Patel | last post by:
Hi, I am looking for script to allow multiple files can be uploaded/attached in webform ( mostly cgi/perl or php). But I don't like the multiple input boxes using "<input type="file" size="40"...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
6
by: RobR | last post by:
We have a customer using our application that has a problem. Within our app, we have a two different forms (one generates an email, the other a fax via a webservice). When they click the submit...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
11
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is...
17
by: Sam Kong | last post by:
Hello, There are 1 or more select elements with the same name in a form. Let's say that the name of the select is 'select1' and the name of the form is 'form1'. If there's only one select I...
4
by: pbd22 | last post by:
Hi. In my script the below code creates a new element on the page with an associated delete button: var row_element = new Element( 'div', { 'class':'container', 'events':{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.