Beruflich Dokumente
Kultur Dokumente
TA: Haining Mo
GUI access
Download WinSCP
MS Windows Users
May code on Windows machines. But Need to test/run programs remotely on Linux machines. Students will need the following:
PuTTY SSH client
http://www.chiark.greenend.org.uk/~sgtatham/putty/ Use it to compile and run your code Use the Linux commands from previous slides
PuTTY
This is what you see when you open putty Dont worry about all the other stuff Just need to know Host Name (or IP address)
For Linux server, its fester.engr.uconn.edu
Keep everything else the same Save the session if you want so you dont have to keep typing in the host name Click Open
PuTTY
You will most likely (or definitely) see this if this is the first time accessing the server. Just click Yes
PuTTY
Lastly, just enter your SoE username and password (the same ones you use to log into the computers in the ITEB and E2 lab). Now, you can run Linux commands from your Windows machine to compile and run your code
6
WinSCP
This is what you see when you open WinSCP (similar to putty actually) Again dont worry about all the other stuff Just need to know Host Name (or IP address)
Again, its fester.engr.uconn.edu
Type in your username (required) Type in your password (optional) Keep everything else the same Save the session if you want so you dont have to keep typing in the host name Click Login
7
WinSCP
Like in putty, you will most likely (or definitely) see the above warning if this is the first time accessing the server. Just click Yes, then enter your password (as shown to the right) if necessary
8
WinSCP
Now, you can access the files and directories on the Linux server like using a Windows Explorer
Can create new files and folders (right click-> new, etc) Drag-and-drop files and folders to and from your Windows machine to the Linux server just like you would between 2 Windows folders
WinSCP
**Quick note: if you ever get a permission denied or access denied error, it means you need to change the permissions on the files on the Linux server Just right-click->properties the file Under Permissions:
R = read W = write X = execute
Basic Utilities
In Unix family everything is a file Directory is a file and also a collection of files
Its like a folder in Windows
Basic Utilities
mkdir create a directory (rmdir deletes)
mkdir [new_directory_name]
cd Change directory
cd [directory_name] (go to specified directory) cd .. (go to previous directory) cd /home/<username> (goes back to main directory)
Creation of a file:
touch [filename] (creates a new empty file) rm [filename] (removes a file) vi [filename] (open file with vi text editor)
12
Basic Utilities
Compile source code into executables
Commands are given later in the C++ section
Example Later
13
Additional Utilities
Many commands also have additional options
Ex: ls -l (more detailed listing of contents)
View a file : cat ,less, more etc User info : who, whoami, finger etc Process info : ps, kill, fg, &, etc Search utilities: grep, find, locate etc Many more commands
http://www.oreillynet.com/linux/cmd/ http://ss64.com/bash/ Google!
14
C++ programming
Great reference for looking up classes, functions, etc
http://www.cplusplus.com/reference/
15
Pointers
* signifies a pointer
type * name declares a pointer variable
type = type of the data that the pointer is pointing to Position of * doesnt matter
type* name == type *name == type * name
int x; // integer x int* ptr = &x; // pointer to address of x * also used to dereference a pointer (to read the value that the pointer is pointing to)
Ex: int j = *ptr; // j = integer x (from above)
16
Pointers
Pointers and arrays:
The arrays identifier is equivalent to the address of its first element
int numbers [20]; int * p;
C++ programming
Refresher from CSE 123
Primitive types (int, float, char, bool) are passed into functions by value
A copy is passed into the function
Ex:
function(int i) { i++ } // function takes in a value and increments it within the function int a = 2; // as value is 2 function(a); // value is incremented // a is still 2 // a is not affected afterwards
function(char * c) { c[0] = a }
Char* txt = street; function(txt);
// function takes in a pointer to a character (array) and changes the first character // txt points to string street // value is incremented // txt now points to string atreet 18
File Access
Theres 2 (and possibly more) ways to open a file for processing Method 1:
FILE * inFile; inFile = fopen(<filename>, "r"); if(inFile == NULL) { error } while(!feof(inFile)) { do stuff } fclose (inFile); Similar steps for writing to file
20
File Access
Method 2:
ifstream inputFile; inputFile.open(<filename>); if(inputFile.is_open()) { while(!traceFile.eof()) { do stuff } } inputFile.close(); Need to keep track of streampos (current position of the pointer in the file)
21
String Processing
char* strcpy(char* dest, const char* src);
different from dest = src (because that would be copying the pointers, not the strings)
src = hello; dest[64] strcpy(dest, src); //dest = hello;
22
23
String Processing
One good way to print a string
#include <stdio.h> printf( const char *format, ); Ex. s1: Dan:9000.1:123
char name[5]; float money = 9000.01; int ID = 123; strcpy(name, Dan); // copy Dan into name printf(My name is %s, ID = %d. I have $%f., name, ID, money);
This will print My name is Dan, ID = 123. I have $9000.01.
25
Compiler = source code object code Linker = object code executable program
26
Compilation Commands
Gcc compiler for c, g++ for c++
gcc [file_name].c -o [executable_name] g++ [file_name].cpp -o [executable_name]
Examples:
g++ main.cpp -c
Creates the object file main.o
Just run the appropriate compilation command However, the file will be very long and difficult to go through
28
Example program
Heres an example program with a lot of the concepts that were explained earlier
Copy/paste everything from ===START=== TO ===END=== (extends over multiple slides) into a file test.cpp
===START===
// compile with "g++ test.cpp -o test" // run with "./test" or "./test [additional arguments]" #include <stdio.h> #include <cstring> void function1(int* a) { // alters the contents of the array a[0] = 99; a[1] = 100; } void function2(int a) { // attempts to alter the passed-in value printf("BEFORE (inside) function2: a = %d\n",a); a += 10; printf("AFTER (inside) function2: a = %d\n",a); }
29
Example program
int main (int argc, char * argv[]) { // prints number of arguments printf("# args = %d\n", argc); // if there is an additional argument, print it if(argc > 1) printf("argument = %s\n", argv[1]); else printf("no additional arguments"); // demo on sscanf and strcpy char *s1 = "sam:9999:123"; // will generate a warning, but ignore it for now char s2[32]; strcpy(s2, s1); // copy s1 into s2 printf("s2: %s\n",s2); char name[5]; int ss; int yr; sscanf(s2,"%[^:]:%d:%d",name,&ss,&yr); printf("name: %s\n",name); printf("ss: %d\n",ss); printf("year: %d\n",yr);
30
Example program
// demo on array (pointers) and functions int list[] = {0,1,2}; printf("BEFORE function1: [%d,%d,%d]\n",list[0], list[1], list[2]); function1(list); printf("AFTER function1: [%d,%d,%d]\n",list[0], list[1], list[2]); // demo on int (primitives) and functions int i = 4; printf("BEFORE (outside) function2: i = %d\n",i); function2(i); printf("AFTER (outside) function2: i = %d\n",i); // demo on pointers, referencing, and dereferencing int *ptr = &i; // create a pointer to int i i++; int j = *ptr; // save in j, the value pointed to by ptr printf("j = %d\n",j); // printf("value pointed to by ptr: %d\n",*ptr); printf("value of ptr (an address): %p\n",ptr);
// comment out either of the following to test int * ptr2 = list; // generates no error //list = ptr; // generates error (because list is a const pointer to array) printf("ptr2: %d\n",ptr2[0]);
return 0; }
===END===
31
Overall Summary
So this is the summary of what some users might do if they needed to write code in Windows but needed to compile and test it remotely on the Linux server:
1. Write all my code using a text editor (like notepad) 2. Open WinSCP and connect to the Linux server 3. Drag-and-drop (to copy/paste) my code from my Windows machine to the Linux server using WinSCP 4. Open PuTTY and connect to the Linux server 5. Navigate to the directory where my code is and use the appropriate commands to compile and run my code using PuTTY 6. If there are errors, make changes to the code on the Windows machine and copy/paste the new code back into the Linux server using WinSCP (overwrite previous files) 7. Repeat as needed
Yes this method is rather cumbersome but thats probably just one possible way. There are definitely others.
32
End
I hope this helps. Good luck! If you have any questions or concerns please feel free to contact me and I will try my best to help.
haining.mo@engr.uconn.edu
33