Posted: . At: 12:13 PM. This was 3 years ago. Post ID: 1988
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Useful C++ snippet. A better way to read in a sentence with STDIN.


This is a very useful C++ code snippet for reading a line of text into STDIN. Hopefully, this will be useful to someone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>
#include <sstream>
 
using namespace std;
 
int goku(void) {
	cout << "Hello, Please enter your name.\n" << endl;
	return 1;
}
 
int main(int argc, char **argv) {
 
	goku();
 
	string name = "";
	getline(cin, name);
 
	cout << "Welcome "<< name << ".\n"<< endl;
 
	return 0;
}

This is another code sample, this is how to get input to a C program with the console. This is how to have command line parameters in a console app.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
int main(int argc, char **argv) {
	struct utsname uname_pointer;
	char* myarg1 = argv[1];
 
	int statvfs(const char *path, struct statvfs *buf);
	int fstatvfs(int fd, struct statvfs *buf);
 
	if (!argc or !myarg1) {
		print_menu();
		testing();
	}
 
	if (argc > 1 and strncmp(argv[1], "1", BUF) == 0) {
/* * The utsname function: */
		uname(&uname_pointer);
		if (strlen (uname_pointer.domainname) < 7 or
		    strncmp(uname_pointer.domainname, "(none)", 10) == 0) {
			printf ("System name : %s \n"		\
				"Nodename    : %s \n"		\
				"Release     : %s \n"		\
				"Version     : %s \n"		\
				"Machine     : %s \n\n",
				uname_pointer.sysname,
				uname_pointer.nodename,
				uname_pointer.release,
				uname_pointer.version,
				uname_pointer.machine
				);
		} else {
			printf ("System name : %s \n"		\
				"Nodename    : %s \n"		\
				"Release     : %s \n"		\
				"Version     : %s \n"		\
				"Machine     : %s \n"		\
				"Domain Name : %s \n\n",
				uname_pointer.sysname,
				uname_pointer.nodename,
				uname_pointer.release,
				uname_pointer.version,
				uname_pointer.machine,
				uname_pointer.domainname
			);
		}
	}
 
	return 0;
}

This code should be very helpful to a person writing a console application on Linux.

This is a very useful code sample. This is how to open a file on Linux to append data to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int files (const char *File, char *f02) {
	FILE *f;
	f = fopen (File, "a+");
 
	if (!f) {
		printf("Sorry, I cannot open the file %s.\n", File);
		return 0;
	}
 
	fprintf(f, "%s", f02);
 
	fflush(stdout);
	fclose(f);
}

And finally, an alternative to printf. This is a simple function to print a text string. This is very easy to use.

1
2
3
4
5
6
7
8
9
#define syscall(a, D, S, d) __asm__ __volatile__("syscall" : : "a"(a), "D"(D), "S"(S), "d"(d))
 
void _starting(void)
{
        syscall(1, 1, "Logfile written.\n", 18);
        syscall(60, 0, 0, 0);
}
 
_starting();

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.