CSE1320 Strings Dr. Sajib Datta CSE@UTA.

Slides:



Advertisements
Similar presentations
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
Advertisements

FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
File I/O.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
File IO and command line input CSE 2451 Rong Shi.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
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:
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
C Programming Lecture 12 : File Processing
Files A collection of related data treated as a unit. Two types Text
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
LINKED LISTS.
Chapter 4.
ECE Application Programming
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.
Lecture 8 String 1. Concept of strings String and pointers
Lecture 11 File input/output
Understand argc and argv
CSE 1320 Search, Sort and Strings
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Module 2 Arrays and strings – example programs.
Computer Science 210 Computer Organization
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
FILE HANDLING.
Computer Science 210 Computer Organization
CSE1320 Files in C Dr. Sajib Datta
Yung-Hsiang Lu Purdue University
File I/O We are used to reading from and writing to the terminal:
C Stuff CS 2308.
רשימות מקושרות עבודה עם קבצים דוגמה
CSE1320 Strings Dr. Sajib Datta
CSC215 Lecture Input and Output.
INC 161 , CPE 100 Computer Programming
CSC215 Lecture Input and Output.
FILE HANDLING 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.
File Handling.
Introduction to Computer Organization & Systems
C Input / Output Prabhat Kumar Padhy
Files.
String manipulation string.h library
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings #include <stdio.h>
ETE 132 Lecture 8 By Munirul Haque.
CS1100 Computational Engineering
Strings Adapted from Dr. Mary Eberlein, UT Austin.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

CSE1320 Strings Dr. Sajib Datta CSE@UTA

String functions strcat() strcpy() strtok()

strcat() strcat() for string concatenation Take two strings for arguments A copy of the second string is tacked onto the end of the first, and this combined version becomes the new first string. The second string is not altered. The return type of strcat () is char *, the value of its first argument – the address of the first character of the string to which the second string is appended.

strcat() cont. #include <stdio.h> #include <string.h> int main(void) { char flower[80]; char addon[] = " is a cat."; scanf("%s", flower); strcat(flower, addon); printf("%s\n",flower); return 0; } Note!!! strcat( ) not checking whether the first array is large enough to hold the second string.

If your input is Kitty, your output is going to be as follow. Kitty is a cat.

strcpy() #include <stdio.h> #include <string.h> int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

strcpy() #include <stdio.h> #include <string.h> int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy+7, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

Output copy - Be the beast orig - beast

strtok() #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence," ."); printf("%s\n",word); char *secondword = strtok(NULL," ."); printf("%s\n",secondword); }

strtok() cont. #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence,"xz"); printf("%s\n",word); char *secondword = strtok(NULL,"xz"); if(secondword==NULL) printf(":)\n"); }

strtok() cont. #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence," ."); while(word!=NULL) { printf("%s\n",word); word = strtok(NULL," ."); }

strtok() cont. Is there anything wrong in this code? #include<stdio.h> #include<string.h> void main(){ char *sentence="Bill is really cool."; char *word = strtok(sentence," ."); while(word!=NULL) { printf("%s\n",word); word = strtok(NULL," ."); }

Use fgets() Not gets() #include <stdio.h> int main() { char name[10]; printf("What’s your name? \n"); fgets(name,10,stdin); printf(“Nice to meet you, %s.\n",name); return(0); }

fopen() and fclose() fopen() function is used to open a file to perform operations such as reading, writing etc. FILE *fp; fp=fopen (“filename”, ”‘mode”); fclose() function closes the file that is being pointed by file pointer fp. fclose(fp);

# include <stdio.h> # include <string.h> int main( ) { FILE *fp ; char data[50]; // opening an existing file printf( "Opening the file test.c in write mode" ) ; fp = fopen("test.c", "w") ; printf( "\n Enter some text to write to the file test.c" ) ; // getting input from user while ( strlen ( gets( data ) ) > 0 ) // writing in the file fputs(data, fp) ; fputs("\n", fp) ; } // closing the file printf("Closing the file test.c") ; fclose(fp) ; return 0;

Output: > gcc  file1.c –o file > ./file  Opening the file test.c in write mode Enter some text to write to the file test.c Hello World!!! Closing the file test.c

File Operations A program has many ways to obtain input data, for example  using scanf to get data from a user through keyboard. using argc and argv to get data from the command line. using file operations to get data stored on a disk.

The following example shows the usage of fscanf() function. #include <stdio.h> #include <stdlib.h> int main () { char str1[10], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs("We are in 2018", fp); rewind(fp); fscanf(fp, "%s %s %s %d", str1, str2, str3, &year); printf("Read String1 |%s|\n", str1 ); printf("Read String2 |%s|\n", str2 ); printf("Read String3 |%s|\n", str3 ); printf("Read Integer |%d|\n", year ); fclose(fp); return(0); }

Output Read String1|We| Read String2 |are| Read String3 |in| Read Integer |2018|

The following program using argv[1] as a file’s name The following program using argv[1] as a file’s name. This programs checks whether argc is at least two to decide whether argv[1] is available. #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc < 2) printf("Need to provide the file's name.\n"); return EXIT_FAILURE; } printf("The name of the file is %s.\n", argv[1]); return EXIT_SUCCESS;

Running the program without the file’s name will produce a message and the program returns EXIT FAILURE. Output: > gcc file1.c -o file1 > ./file1 Need to provide the file’s name. If the file’s name is given, the program prints the file’s name: > ./file1 data The name of the file is data.

Read Character by Character Using fgetc Read Character by Character Using fgetc. This program counts the number of characters in the input file. #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { FILE * fptr; int ch; int counter = 0; fptr = fopen(argv[1], "r"); printf("The name of the file is %s.\n", argv[1]); do ch = fgetc(fptr); if (ch != EOF) counter ++; } } while (ch != EOF); fclose(fptr); printf("The file has %d characters.\n", counter); return EXIT_SUCCESS;

Output: > gcc  file2.c -o file2 > ./file2 file2.c The name of the file is file2.c The file has 716 characters.

A sample program to read input from a file and store the output to an another file. #include <stdio.h> #include <stdlib.h> // For exit() function int main() { char c[1000]; FILE *fptr; if ((fptr = fopen(“program.txt”, “r”)) == NULL) printf("Error opening file!"); exit(1); } // reads text until newline fscanf(fptr,"%[^\n]", c); printf("Data from the file:\n%s", c); fclose(fptr); return 0;

> cat program.txt Welcome to Intermediate Programming Course We are learning I/O and files. Have fun coding! Output > gcc file.c -o file > ./file >> output.txt > cat output.txt Data from the file:

A sample C program to create a file and write data into file. #include <stdio.h> #include <stdlib.h> #define DATA_SIZE 1000 int main() { /* Variable to store user content */ char data[DATA_SIZE]; /* File pointer to hold reference to our file */ FILE * fptr; /* Open file in w (write) mode.*/ fptr = fopen("file1.txt", "w"); if(fptr == NULL) /* File not created hence exit */ printf("Unable to create file.\n"); exit(EXIT_FAILURE); }

/* Input contents from user to store in file */ printf("Enter contents to store in file : \n"); fgets(data, DATA_SIZE, stdin); /* Write data to file */ fputs(data, fptr); /* Close file to save file data */ fclose(fptr); printf("File created and saved successfully. \n"); return 0; }

Output > gcc sample.c -o sample >./sample Enter contents to store in file :  Hello World! This is a sample text file File created and saved successfully. > vi file1.txt  This is a sample text file