0% found this document useful (0 votes)
3 views

CS 604P Asignment 1

The document contains a C program that utilizes multithreading to perform two tasks: calculating the sum of integers from an input file and modifying a user-provided string by replacing vowels with asterisks. It checks for the existence of an input file and creates one with sample data if it doesn't exist. The results of both tasks are written to an output file and printed to the console.

Uploaded by

furqanhaider5t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CS 604P Asignment 1

The document contains a C program that utilizes multithreading to perform two tasks: calculating the sum of integers from an input file and modifying a user-provided string by replacing vowels with asterisks. It checks for the existence of an input file and creates one with sample data if it doesn't exist. The results of both tasks are written to an output file and printed to the console.

Uploaded by

furqanhaider5t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment # 01

CS604P

Name: Furqan Haider


VUID: bc230401281

Screenshot:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <ctype.h>

#define INPUT_FILE "input.txt"


#define OUTPUT_FILE "output.txt"

int sum = 0;
char name[100];
char modified[100];

void* thread1_func(void* arg)


{
FILE* fp = fopen(INPUT_FILE, "r");
if (!fp) {
perror("Failed to open input.txt");
pthread_exit(NULL);
}

int num;
while (fscanf(fp, "%d", &num) == 1)
{
sum += num;
}
fclose(fp);

FILE* out = fopen(OUTPUT_FILE, "w");


if (!out)
{
perror("Failed to open output.txt");
pthread_exit(NULL);
}

fprintf(out, "T1: Sum of numbers: %d\n", sum);


fclose(out);

printf("T1: Sum of numbers: %d\n", sum);


pthread_exit(NULL);
}

void* thread2_func(void* arg)


{
printf("T2: Enter a string: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';

strcpy(modified, name);
for (int i = 0; modified[i] != '\0'; i++)
{
char ch = tolower(modified[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
modified[i] = '*';
}
}

printf("T2: Modified string: %s\n", modified);

FILE* out = fopen(OUTPUT_FILE, "a");


if (!out)
{
perror("Failed to open output.txt");
pthread_exit(NULL);
}

fprintf(out, "T2: Modified string: %s\n", modified);


fclose(out);
pthread_exit(NULL);
}

int main()
{
FILE* check = fopen(INPUT_FILE, "r");
if (!check)
{
FILE* create = fopen(INPUT_FILE, "w");
if (!create)
{
perror("Failed to create input.txt");
return 1;
}
fprintf(create, "10\n20\n30\n40\n50\n");
fclose(create);
}
else
{
fclose(check);
}

pthread_t t1, t2;


pthread_create(&t1, NULL, thread1_func, NULL);
pthread_join(t1, NULL);

pthread_create(&t2, NULL, thread2_func, NULL);


pthread_join(t2, NULL);

return 0;
}

You might also like