Cs288 Intensive Programming in Linux

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Introduction to C Programming
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File Handling Spring 2013Programming and Data Structure1.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Chapter 18 I/O in C.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
CS 1704 Introduction to Data Structures and Software Engineering.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
Cs288 Intensive Programming in Linux
LINKED LISTS.
Cs288 Intensive Programming in Linux
Cs288 Intensive Programming in Linux
Cs288 Intensive Programming in Linux
A bit of C programming Lecture 3 Uli Raich.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Lecture 11 File input/output
Day 02 Introduction to C.
An Introduction to C Programming
Command Line Arguments
Chapter 18 I/O in C.
Cs288 Intensive Programming in Linux
Introduction to C CSE 2031 Fall /3/ :33 AM.
Cs288 Intensive Programming in Linux
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Short Overview Lembit Jürimägi.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Computer Science 210 Computer Organization
CS111 Computer Programming
Computer Science 210 Computer Organization
Hank Childs, University of Oregon
File I/O We are used to reading from and writing to the terminal:
Command Line Arguments
C Stuff CS 2308.
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
sscanf()- string scan function
Chapter 18 I/O in C.
Programming and Data Structure
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
File Input and Output.
Line at a time I/O with fgets() and fputs()
C Input / Output Prabhat Kumar Padhy
Introduction to C EECS May 2019.
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Chapter 18 I/O in C.
Presentation transcript:

Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkowski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 8 27-10-16

today homework revu scanf Pointers Bit operations FILE operations Macros Typedefs malloc() and free() Linked lists C lib Recursion In class exercises 6-9-15 cfy

homework Do exercise 1-19 from the text. Redo problem 4 from class 2 as a C program Write a program which reads stdin and converts any local case characters to upper 6-9-15 cfy

1-19 write a function called reverse(s) which reverses the character string s. Use it to write a program which reverses its input. solution at: https://web.njit.edu/~yurkowsk/x/reverse.c 6-9-15 cfy

6-9-15 cfy

6-9-15 cfy

6-9-15 cfy

1-21 write a program that replaces blanks with the minimum number of tabs and blanks. soluton at: https://web.njit.edu/~yurkowsk/x/entab.c 6-9-15 cfy

27-10-16 cfy

Problem3 https://web.njit.edu/~yurkowsk/x/prob3A.c 27-10-16 cfy

toupper.c https://web.njit.edu/~yurkowsk/x/toupper.c #include <stdio.h> main(){ char c; while((c=getchar())!=EOF){ if(c>='a' && c <= 'z') c+='A'-'a'; putchar(c); } 27-10-16 cfy

scanf Format similar to printf %d, %s, %x etc. int scanf(const char *restrict format, ... ); %d, %s, %x etc. 27-10-16 cfy

example #include <stdio.h> main(){ char action[6]; int word; while(scanf("%s %d",action,&word)!=EOF){ fprintf(stderr,"%s %d\n",action,word); if(!strcmp(action,“DO")) fprintf(stderr, “do something%x\n",word); else “reverse action %x\n",word); } 27-10-16 cfy

>: echo “DO 666" |. /a. out add 10 add a >: echo “UNDO 7" |. /a >: echo “DO 666" | ./a.out add 10 add a >: echo “UNDO 7" | ./a.out delete 7 27-10-16 cfy

pointers char c='a', d='b', *cp; cp= &d; *cp=c; printf("%c %c\n",c,d); 27-10-16 cfy

Referencing structures typedef struct string string; struct string{ struct string *forward; int data; }a[SZ]; a[i].forward=&a[i+1]; 27-10-16 cfy

Using pointers w/ structures struct list{ struct list *l; int payload; }array[SIZE]; struct list *lp;   lp=array; lp->payload=data[10]; 27-10-16 cfy

BIT OPERATIONS int x, y, z; x&=7; z=y|x; z=y<<2; if(y^x); x=~y; 27-10-16 cfy

FILE I/O fopen() and friends (fwrite(), fread(), fseek() etc. 27-10-16 cfy

FILE *f; f=fopen("/tmp/yurkoski","w"); fprintf(f,“msg"); 27-10-16 cfy

Code macros e.g.: #define sum(a,b) (a+b) #define macro-name(args) (expression) e.g.: #define sum(a,b) (a+b) 6-9-15 cfy

#include <stdio.h> #include <stdlib.h> #define cube(a) (a*a*a) /* no semicolon*/ main(int argc, char *argv[]) { int a; if(argc != 2){ fprintf(stderr, "usage: %s n\n",argv[0]); exit(-1); } a=atoi(argv[1]); printf("the cube of %d is %d\n", a, cube(a)); 6-9-15

Revu typedefs Used to define you own types syntax: typedef {structure-def}typedef-name; Usage: typedef-name variable-name;

typedef struct { int fill; char name[8]; }frame; frame *p; p=(frame *)malloc(SIZE*sizeof(frame)); p++; p->fill=fill;

#include <stdlib.h> #define SIZE 10 typedef struct { int fill; char name[8]; }frame; main() { frame *p, *q, fill; char cmd[10]; q=p=malloc(SIZE*sizeof(frame)); 6-9-15

Linked list Singly linked lists Doubly linked lists Circularly linked lists Multiply linked lists 6-9-15 cfy

Singly linked lists 6-9-15 cfy

Doubly linked lists 6-9-15 cfy

C library atoi() itoa() atof() 27-10-16 cfy

Recursion 27-10-16 cfy

In class problem 1 Write a C program which accepts as a command line argument an integer and reads another integer from standard input and bit wise or the two together and writes the result (converted to an ascii string) to a file in the in /tmp whose name is your student id number. Put your source code in moodle.

example if the file /tmp/x contains a 5, and your id is 666 cc yourprogm.c –o yourprogm cat /tmp/x | yourprogm 6 should write the ascii string 30 to the file 666 in the current directory.

In class problem 2 Write a program which reads a series of integers for standard input until an EOF is encountered. Store them in a linked list in sorted order. Upon encountering the EOF, output the items on the list.