Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

C++ system() and variables?

Original Message
Name: tImmaY
Date: December 7, 2004 at 00:14:40 Pacific
Subject: C++ system() and variables?
OS: Windows XP Home
CPU/Ram: AMD Athlon XP 2400+ / 512
Comment:
Hello, i'm just messing around right now in C++ and i'm trying to make my own little "search" utility b/c the one in windows xp is horribly slow and i've found with a batch file you can find the location of something like 100x faster then windows search can. so now i'm just trying to write a little program that will be more flexible then going into a batch file and changing a file name every time i want to find something :) however, for this i need to take in a variable and then include that in the system() command as the string. but i'm not sure how i'd do this.. as of now i tried:

system("dir /s /b | find /i \"" << strng << "\" > \"c:\\shibby\\foundfile.shibby\"");

but Dev-C++ tells me: "error: invalid
operands of types `const char[22]' and `char[50]' to binary `operator<<'
" (i hope that displays properly. we'll see in a minute.. but yea, does anyone know how i'd accomplish this task?


Report Offensive Message For Removal


Response Number 1
Name: tImmaY
Date: December 7, 2004 at 01:25:42 Pacific
Subject: C++ system() and variables?
Reply: (edit)
yea so i figured out it would be easier to just have the program create a batch file and put the string into that, then run the batch file through the program. once its done searching, it deletes the batch file. so NOW, how do i make the program return to the main menu? because i was once told by someone that you shouldn't directly call the main() function from another function.. and he produced a file that would do this very thing. but he used a switch and bool statement in it and i can make switch statements (i used one in this file) but i'm not positive how to make it produce this same result and unluckily, i dont still have this file :(

Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: December 7, 2004 at 06:10:09 Pacific
Subject: C++ system() and variables?
Reply: (edit)
Need more input.

We need to see your program to answer this question.

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 3
Name: tImmaY
Date: December 7, 2004 at 08:34:17 Pacific
Subject: C++ system() and variables?
Reply: (edit)
That's doable. Also, if I'm using something wrong or incorrect, please let me know :)

/* A personal test / replacment for Windows Search utility */
#include <iostream>
#include <fstream>
using namespace std;

void find();
void view();

int main()
{
int option;

system("title Shibby Inc. (c) 2004");
cout << "What would you like to do?" << endl;
cout << "1 - Find a File." << endl;
cout << "2 - View the locations of the file specified." << endl;
cout << "3 - Quit.\n_\b";
cin >> option;

switch(option)
{
case 1 : find();
break;
case 2 : view();
break;
case 3 : cout << "\n\n(C) Shibby Inc. 2004 - Thank you :)" << endl;
system("pause");
return 0;
break;
default : cout << "Please only make one of the choices given." << endl;
system("pause");
}
return 0;
}

void find()
{
char strng[25]; //If there's a string being searched bigger then this
//Then the search is too specific & can be done by Windows :)
cout << "\n\nPlease input the string you'd like to find:" << endl;
cin >> strng;
system("@echo off"); //Just for the poo dilly of it
ofstream shibby("search.bat", ios::trunc);
shibby << "@echo off" << endl;
shibby << "cd\\" << endl;
shibby << "rd /s /q shibby" << endl; //In case the directory exists, deletes it + anything in it w/o asking for confirmation.
shibby << "mkdir shibby" << endl; //Remakes the directory
shibby << "dir /s /b | find /i \"" << strng << "\" > c:\\shibby\\foundfile.shibby" << endl;
shibby.close();
cout << "\nSearching..." << endl;
cout << "Don't worry, the program didn't freeze." << endl;
system("search.bat");
system("del search.bat"); //Once the search is done, there's no need for this to exist.
}

void view()
{
system("notepad.exe c:\\shibby\\foundfile.shibby"); //Will come up with nothing if no string was found.
}


Report Offensive Follow Up For Removal

Response Number 4
Name: Don Arnett
Date: December 7, 2004 at 09:35:54 Pacific
Subject: C++ system() and variables?
Reply: (edit)
First, to answer the question about how to get back to the menu, add this code to your main. I've put *** at the start of lines that I've added, so you'll need to remove the ***:

int main()
{
int option;
***char doMore = 1;

***while (doMore) {

system("title Shibby Inc. (c) 2004");
cout << "What would you like to do?" << endl;
cout << "1 - Find a File." << endl;
cout << "2 - View the locations of the file specified." << endl;
cout << "3 - Quit.\n_\b";
cin >> option;

switch(option)
{
case 1 : find();
break;
case 2 : view();
break;
case 3 : cout << "\n\n(C) Shibby Inc. 2004 - Thank you :)" << endl;
system("pause");
***doMore = 0;
return 0;
break;
default : cout << "Please only make one of the choices given." << endl;
system("pause");
}

***} // end of while loop

return 0;
}


The above changes add a while loop that loops until option three is selected.

while (doMore) {

is the same as:

while (doMore != 0) {

Looking thru your code, I noted:


system("title Shibby Inc. (c) 2004");
The system command sends the string as a command to run on the Windows system. So this is trying to run a command named 'title'. I'm guessing that what you really want here is to echo the output.

One more thing:

void find()
{
char strng[25];

cout .....
cin >> strng;


cin will allow the user to input more than 25 characters. If they do, the extra characters will be stored as if the array were big enough to hold them. This could cause problems in the program because you'll be overwriting memory on the stack (an advanced topic).

I don't know the proper C++ way to prevent this. I use a C input function to handle this (it'll work in C++).

char strng[25];
fgets(strng, 25, stdin);
if (strng[ strlen(strng)-1 ] == '\n') {
strng[ strlen(strng)-1 ] = '\0';
}

This will input until 24 characters are read (leaving 1 char for null) or until a newline is read. Because of the 'stdin', it is reading from standard in (normally the keyboard). fgets also leaves the newline on, so the if checks for and removes the newline if it is there.

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 5
Name: tImmaY
Date: December 7, 2004 at 12:08:26 Pacific
Subject: C++ system() and variables?
Reply: (edit)
thank you :)

but the system("title Shibby Inc. (c) 2004"); makes the title of the command prompt the rest of the string :) lol, so instead of displaying the path that the file is being run from, thats what is displayed.

also, i'm trying to start learning C because i'm taking a C Programming class next semester and i like to have knowledge of what i'm going to be doing before i do it. lol so i've been reading the about.com tutorial on C in my spare time.. but is there a better one that you would recommend? the one i'm referring to is here: http://cplus.about.com/od/beginnerctutoria1/l/blctut.htm

thanks


Report Offensive Follow Up For Removal


Response Number 6
Name: tImmaY
Date: December 7, 2004 at 20:45:56 Pacific
Subject: C++ system() and variables?
Reply: (edit)
ok, i made the specified changes and tested it :) its still faster then Windows search utility. lol although theirs IS more advanced (shows icons, folders only once instead of everything inside a folder as being part of the search criteria, etc.) but mines faster ^_^ haha that was fun.. now to play with it and see if i can include an icon & such...

Report Offensive Follow Up For Removal

Response Number 7
Name: Wolfbone
Date: December 7, 2004 at 22:19:51 Pacific
Subject: C++ system() and variables?
Reply: (edit)
"i've been reading the about.com tutorial on C in my spare time.. but is there a better one that you would recommend?"

"The C Book" 2nd edition:

http://publications.gbdirect.co.uk/c_book/

There was also a truly excellent tutorial series on C by Steven Goodwin in Linux Magazine (issues 14-26). It is available in 13 pdf files from their archive:

http://www.linux-magazine.com/Magazine/Archive


Report Offensive Follow Up For Removal

Response Number 8
Name: Don Arnett
Date: December 8, 2004 at 10:12:13 Pacific
Subject: C++ system() and variables?
Reply: (edit)
"i'm trying to start learning C because i'm taking a C Programming class next semester "

Are you learning C or C++?? cin/cout are C++, not C. Everything (almost everything?) in C works in C++, but not the other way around.

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 9
Name: Cwy
Date: January 2, 2005 at 08:01:50 Pacific
Subject: C++ system() and variables?
Reply: (edit)
hi. im wondering...does anyone knows of a website which has information for system() or just how to open and extract zip files?

Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++ system() and variables?

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC