LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2018, 08:29 PM   #1
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Rep: Reputation: Disabled
Program Won't Run After ./a.out


So I'm having some issues, I just started using linux but I'm fairly certain I'm doing this right.

I navigate to my documents folder by typing: cd Documents/
Then, I open my gedit: gedit stringReplacement.cpp
Then, I add my code for my homework and save it, closing the gedit window.
Next, I type sudo g++ stringReplacement.cpp
I notice that a file a.out appears in my Documents folder.
I try to run the program by typing ./a.out

Cursor gets stuck one line below, but it seems like a process is running because when I try to exit the terminal, it asks me if I want to kill the process.

Can anyone tell me what I'm doing wrong?
 
Old 12-05-2018, 08:52 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,551

Rep: Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569
From what I'm reading?

You're doing nothing wrong. But there might be a bug in the program you're writing for homework.

You can press Ctrl+C to quit out of your program (if you didn't already know that).
 
Old 12-05-2018, 09:07 PM   #3
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
my friend in the class worked on it with me and his is working perfectly fine. during class we tried moving it to another directory and it ran once, then got the same issues. I just have no idea whats wrong with it, because it compiles fine, and some process is ongoing clearly.

edit: ok so there was an issue with my code. I was trying to link the entire directory for the txt file like "/home/Documents/strRep.txt". After removing that it runs, just not correctly.

The file is in the current directory, I should just be able to use "strRep.txt" right? Not sure why it's now not finding it.

Last edited by ryan17stehle; 12-05-2018 at 09:21 PM.
 
Old 12-06-2018, 04:37 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19 MATE
Posts: 6,410
Blog Entries: 2

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
You'll probably need to paste the code here for folk to help with specific code issues.

If you do so, please enclose the code within CODE tags (please see https://www.linuxquestions.org/quest...og.php?b=36313).
 
Old 12-06-2018, 08:30 AM   #5
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 531

Rep: Reputation: 249Reputation: 249Reputation: 249
Hi,

Welcome to LQ.

There can be two issues here. An obvious one is that the C++ code inside stringReplacement.cpp is not doing its job. The other issue might be related with the sudo. I don't see why you need sudo to run a compiler. Now I'm not sure about this, but this may be is causing the issue? Maybe?

Anyway, one advice: when you run compilers on Linux (especially the g.. ones), just pass it the argument -o so that it outputs the executable with another name than "a.out". Imagine you are compiling several programs, the a.out will be overwritten everytime g++ is executed. So, please adopt the following:
Code:
g++ -o OUTPUTEXE SOURCE

e.g.
g++ -o stringReplacement stringReplacement.cpp
 
Old 12-06-2018, 08:36 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 7,341

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
the actual source code so to see if any "bugs" in it to prevent it from starting.
 
Old 12-06-2018, 11:01 AM   #7
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Code:
/* 
	PROGRAMMING PROJECT #3
	Ryan Stehle
	CECS 326 OPERATING SYSTEMS


	Implement a string replacement software and count the number of times some given
	string is replaced in a plain text document.

 */

#include <stdio.h>
#include <sys/types.h> //contains definitions that help manage processes and threads
#include <sys/wait.h> //contains declarations for waiting
#include <unistd.h>  //standard symbolic constants and types
#include <string>
#include <iostream>
#include <fstream>

#define MAX 301 
using namespace std;
int main()

{
	
	ifstream infile("stringReplacement.txt");
	string  fileObject[MAX];


	int index = 0;
	if(infile.is_open())
	{
	
		char s;
		int row = 0;
		string temp = "";

		while(row  < MAX)
		{
			s = infile.get();
			
			while (s != ' ')
			{
				if ((s >= 65 && s <91) || (s>=97 && s <=122) )
				{
				fileObject[row].push_back(s);  // inserts the next character into the next position of a string array
	
				}
			

				s = infile.get();
				
			}
			
			
			row++; // saves next word into the next array with period and such if it exists
		}

	}
	infile.close();	

	for (int t = 0 ; t <MAX; t++)
	{
		cout << fileObject[t] << " ";
		
	}
	string target;
	string replace;
	string exit = "!wq";
	cout << "Please Enter the String You wish to replace or to exit enter !wq " << endl;
	getline(cin, target);
	if( target.compare(exit) == 0)
	{
		cout << "Exiting Program... Entered !wq" << endl;
		return 0;
	}
	cout << "Now enter the NEW  string you wish to replace the old string with " << endl;
	getline(cin, replace);



bool test = true;
while(test)
{
	pid_t  pid;	// pid_t used for process IDs and process group IDs... need unistd.h and sys/types to use functions to getpid(	
	pid = fork();
	if(pid <0)
		 {
			fprintf(stderr, "Fork Failed"); //fprintf =file print 
			 return 1;
		 }
	else if ( pid==0)
	{
		cout << "The childs process's ID: " << getpid() << endl;
		int instance = 0;
		cout << replace << endl;
        	while(instance ==0)
		{
		int row = 0;
		for(; row < MAX; row++)
                {
			
                        int count = 0;
                        int column =0;
                        for(; column < fileObject[row].length() ; column++)
                        {
                                if (fileObject[row][column] == target[column])
                                {
                                        count++;
                                }
                        }
                        if(count == target.length()&& count == fileObject[row].length())
                        {
                                instance++;
                                fileObject[row] = replace;
                        }
                        if(count > 0)
                        {
                                if(fileObject[row][column+1]=='.')
                                {
                                        cout <<"replace this row" << endl;
                                }

                        }
			
		}
			if(instance == 0)
			{
			cout << "." << getpid();
			}
		} //injected bug
        
		
		for(int y=0; y<MAX; y++)
		{	
	        	cout << fileObject[y] << " ";
		}
		
		cout << "\nInstances Found " << instance << endl;
		test = false;
	}
	else
	{
		cout << "Parent Process ID " << getpid() << endl;
		wait(NULL);
		printf("Child Complete");
		cout << "\n" << endl;
		cout << endl;
       	 	cout << "Please Enter the String You wish to replace or !wq to exit " << endl;
       		getline(cin, target);
       
	       	if( target.compare(exit) == 0)
       		 {
                	cout << "Exiting Program... Entered !wq" << endl;
                	test = false;
			return 0;
       		 }
        	cout << "Now enter the NEW  string you wish to replace the old string with " << endl;
        	getline(cin, replace);

	}
}
	return 0;
}
any help would be greatly appreciated.
 
Old 12-06-2018, 11:10 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 7,341

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
works for me
compiled
Code:
g++ homework.cpp -o hmwk
output,
Code:
userx@MinTy:~$ ./hmwk
Please Enter the String You wish to replace or to exit enter !wq                                                                                                                                                                                                                   
 Exiting Program... Entered !wq
if I type in something (the strings), then it goes into an infant loop outputting numbers.

additional info
Code:
userx@MinTy:~$ ls -la hmwk
-rwxrwxr-x 1 userx userx 19416 Dec  6 10:08 hmwk

userx@MinTy:~$ file hmwk
hmwk: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=0e2b49a0a0d33006e1965f4ca90eba9a4aa231f6, not stripped

Last edited by BW-userx; 12-06-2018 at 11:22 AM.
 
Old 12-06-2018, 11:22 AM   #9
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
works for me
compiled
Code:
g++ homework.cpp -o hmwk
output,
Code:
userx@MinTy:~$ ./hmwk
Please Enter the String You wish to replace or to exit enter !wq                                                                                                                                                                                                                   
 Exiting Program... Entered !wq
if I type in something (the strings), then it goes into an infant loop outputting numbers.
that's the point, it's supposed to replace a string from a text file I'm linking, and if the target string isn't in the file, it's supposed to loop infinitely so we can kill -10 the process id

edit: i should clarify, the reason I don't think it's working is because it now loops infinitely regardless of whether the string I want to replace is in the file.

Last edited by ryan17stehle; 12-06-2018 at 11:24 AM.
 
Old 12-06-2018, 11:30 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 7,341

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
Quote:
Originally Posted by ryan17stehle View Post
that's the point, it's supposed to replace a string from a text file I'm linking, and if the target string isn't in the file, it's supposed to loop infinitely so we can kill -10 the process id
that's what I figured no in file, but it still runs.
Quote:
Originally Posted by ryan17stehle View Post
edit: i should clarify, the reason I don't think it's working is because it now loops infinitely regardless of whether the string I want to replace is in the file.
so this is about it is not running after you compiled it yes? and not about having an infant loop, yes?

and on that subject I'd not resort to having to kill it. but have it exit after a fail state.


adding that file for it to open just causes the program to hang in your , run this and see your output
Code:
/* 
	PROGRAMMING PROJECT #3
	Ryan Stehle
	CECS 326 OPERATING SYSTEMS


	Implement a string replacement software and count the number of times some given
	string is replaced in a plain text document.

 */

#include <stdio.h>
#include <sys/types.h> //contains definitions that help manage processes and threads
#include <sys/wait.h> //contains declarations for waiting
#include <unistd.h>  //standard symbolic constants and types
#include <string>
#include <iostream>
#include <fstream>


#define MAX 301 
using namespace std;
int main()

{
	
	ifstream infile("stringReplacement.txt");
	string  fileObject[MAX];
	cout<<"1"<<endl;
	int index = 0;
	if(infile.is_open())
	{
		cout<<"2"<<endl;
		char s;
		int row = 0;
		string temp = "";
	 
		while(row  < MAX)
		{
			cout<<"3"<<endl;
			s = infile.get();
			cout<<s<<endl;
		//while (s != ' ')
			while (infile.good())
			{ 
				cout<<"4"<<" "<<s<<endl;
				if (( s >= 65 && s <91) || ( s >=97 &&  s <=122) )
				{
				fileObject[row].push_back(s);  // inserts the next character into the next position of a string array
	
				}
			

				s = infile.get();
				
			}
			
			
			row++; // saves next word into the next array with period and such if it exists
		}

	}
	 
	else
	{
		cout<<"file not opended."<<endl;
		return -1;
	}
	infile.close();	

	for (int t = 0 ; t <MAX; t++)
	{
		cout << fileObject[t] << " ";
		
	}
	string target;
	string replace;
	string exit = "!wq";
	cout << "Please Enter the String You wish to replace or to exit enter !wq " << endl;
	getline(cin, target);
	if( target.compare(exit) == 0)
	{
		cout << "Exiting Program... Entered !wq" << endl;
		return 0;
	}
	cout << "Now enter the NEW  string you wish to replace the old string with " << endl;
	getline(cin, replace);



bool test = true;
while(test)
{
	pid_t  pid;	// pid_t used for process IDs and process group IDs... need unistd.h and sys/types to use functions to getpid(	
	pid = fork();
	if(pid <0)
		 {
			fprintf(stderr, "Fork Failed"); //fprintf =file print 
			 return 1;
		 }
	else if ( pid==0)
	{
		cout << "The childs process's ID: " << getpid() << endl;
		int instance = 0;
		cout << replace << endl;
        	while(instance ==0)
		{
		int row = 0;
		for(; row < MAX; row++)
                {
			
                        int count = 0;
                        int column =0;
                        for(; column < fileObject[row].length() ; column++)
                        {
                                if (fileObject[row][column] == target[column])
                                {
                                        count++;
                                }
                        }
                        if(count == target.length()&& count == fileObject[row].length())
                        {
                                instance++;
                                fileObject[row] = replace;
                        }
                        if(count > 0)
                        {
                                if(fileObject[row][column+1]=='.')
                                {
                                        cout <<"replace this row" << endl;
                                }

                        }
			
		}
			if(instance == 0)
			{
			cout << "." << getpid();
			}
		} //injected bug
        
		
		for(int y=0; y<MAX; y++)
		{	
	        	cout << fileObject[y] << " ";
		}
		
		cout << "\nInstances Found " << instance << endl;
		test = false;
	}
	else
	{
		cout << "Parent Process ID " << getpid() << endl;
		wait(NULL);
		printf("Child Complete");
		cout << "\n" << endl;
		cout << endl;
       	 	cout << "Please Enter the String You wish to replace or !wq to exit " << endl;
       		getline(cin, target);
       
	       	if( target.compare(exit) == 0)
       		 {
                	cout << "Exiting Program... Entered !wq" << endl;
                	test = false;
			return 0;
       		 }
        	cout << "Now enter the NEW  string you wish to replace the old string with " << endl;
        	getline(cin, replace);

	}
}
	return 0;
}

Last edited by BW-userx; 12-06-2018 at 12:03 PM.
 
Old 12-06-2018, 12:04 PM   #11
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
that's what I figured no in file, but it still runs.


so this is about it is not running after you compiled it yes? and not about having an infant loop, yes?

and on that subject I'd not resort to having to kill it. but have it exit after a fail state.
I have a plaintext file in my documents folder called "stringReplacement.txt". My infile opens "stringReplacement.txt", with which I want to then enter in a string to replace within that file with another string.

If that string is found within the "stringReplacement.txt" file, it replaces it, then goes back to the original prompt. If it doesn't our instructor wants us to kill the process through the second terminal.

For whatever reason, if my text file "stringReplacement.txt" is found, the program just does nothing. If I change the infile to "strRep.txt" for instance (which doesn't exist), the program actually runs through the prompt then infinitely loops.

Last edited by ryan17stehle; 12-06-2018 at 12:07 PM.
 
Old 12-06-2018, 12:14 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 7,341

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
a fix: you have to use int not char if you're taking in int and comparing it to a value
Code:
#include <iostream>     
#include <fstream>      

int main () {
  std::ifstream theFile ("stringReplacement.txt");

  if (theFile.is_open()) {
     
    int s ; 
    while (theFile >> s) {
	if (( s >= 65 && s <91) || ( s >=97 &&  s <=122) )
	{
	        //fileObject[row].push_back(s);  // inserts the next character into the next position of a string array
		std::cout << s <<std::endl;
	}
	else if ( s <= 30)
	{
		std::cout<<s<<std::endl;
	}
		     
	}
  }
  else {
    // show message:
    std::cout << "Error opening file";
  }

  return 0;
}

Last edited by BW-userx; 12-06-2018 at 12:16 PM.
 
Old 12-06-2018, 12:24 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 7,341

Rep: Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477Reputation: 1477
Quote:
Originally Posted by ryan17stehle View Post
I have a plaintext file in my documents folder called "stringReplacement.txt". My infile opens "stringReplacement.txt", with which I want to then enter in a string to replace within that file with another string.

open output file to output to.
humm reading further.....
Quote:
Originally Posted by ryan17stehle View Post
If that string is found within the "stringReplacement.txt" file, it replaces it, then goes back to the original prompt. If it doesn't our instructor wants us to kill the process through the second terminal.

For whatever reason, if my text file "stringReplacement.txt" is found, the program just does nothing. If I change the infile to "strRep.txt" for instance (which doesn't exist), the program actually runs through the prompt then infinitely loops.
1. have file called "stringReplacement" it contains words or strings, and if matched to the in put off then cli then replace it where? into a different file?

you are using int for a char, is that to match the char return value, or it is suppose to?

string comparing should be used, should it not, when looking for a match pertaining to strings?

running a program in one term and killing it in another is just pkill, or killall app name.

your logical flow is a little off, I'd think. by looking at your out put asking for input,
input sting
input another string you want to use to replace prior string.

is this basically it?

open output file, request string to be taken in off the terminal, write to out file, rewind file or close file, take in string to replace first string, open file or loop through output file that has be rewound. find position needed then replace with new string at that position(s). close file. leave handing in a loop so it can be terminated in another terminal by a different means.

or one file has strings in it already that should be presented to the user to pick from, then use tells program what line to replace with what word to use to replace it with, then write to output file, leave program hanging to be terminated by a different means.
Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	ofstream outFile; 
	outFile.open("MeFile", ios::out | ios::in | ios::app);
	 
	if (outFile.is_open())
	{	

			string target;
			string replace;
			string exit = "!wq";
			cout << "Please Enter the String You wish to replace or to exit enter !wq " << endl;
			getline(cin, target);
			 
			if( target.compare(exit) == 0)
			{
				cout << "Exiting Program... Entered !wq" << endl;
				return 0;
			}
			outFile<<target<<endl;
			outFile.close();
						
			ifstream inFile;
			inFile.open("MeFile");
			if (inFile.is_open())
			{
				cout << "Now enter the NEW  string you wish to replace the old string with " << endl;
				getline(cin, replace);
				
				string inout;
				while (inFile >> inout)
				{ 	
					if (inout.compare(replace) != 0)
					{
						cout<<"no match"<<endl;
					}
					else
					{
						cout<<inout<<" "<<replace<<endl;
					}
					
				}
			}
		 }
		
	
	return 0;
}

Last edited by BW-userx; 12-06-2018 at 01:43 PM.
 
Old 12-06-2018, 02:27 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,551

Rep: Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569Reputation: 3569
ryan17stehle: do you know how to compile your program with debugging symbols, and then use a debugger?

Have you tried that?

Would you like to be told how?
 
1 members found this post helpful.
Old 12-06-2018, 02:44 PM   #15
ryan17stehle
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
ryan17stehle: do you know how to compile your program with debugging symbols, and then use a debugger?

Have you tried that?

Would you like to be told how?
I haven't tried that, and sure, i'd like to learn.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
program taken out of book won't run doughyi8u Programming 12 09-04-2014 03:46 AM
Auto run and only run a program after login? narke Slackware 2 05-27-2011 01:51 PM
LXer: Can't Program, won't Program? Then Mash the Web with Mozilla's Ubiquity LXer Syndicated Linux News 0 05-01-2010 01:30 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
How to run a program in an ssh session that will run after you close out the session? dr_zayus69 Linux - Networking 5 03-05-2006 08:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration