Ege University Electrical and Electronics Engineering Introduction to Computer Programming Laboratory Lab 12 - Text IO 1) Working Directory Create a file named Question1.txt and write Hello World! to the file. Run the program within CodeBlocks, and then find the file and check its contents with Notepad. Run executable file located in Debug folder and see the newly created Question1.txt file in the same folder as the executable. Pay attention to two Question1.txt files created at different folders by running same program from different ways. 1) Çalışma Klasörü Soru1.txt adında bir dosya oluşturun ve dosyaya Merhaba Dunya! yazın. Programı CodeBlocks içerisinden çalıştırın, dosyayı NotePad ile açın ve içeriğini control edin. Debug klasöründeki exe dosyasını çalıştırın. Exe dosyası ile aynı klasörde oluşan Soru1.txt dosyasını fark edin. Aynı programın farklı yollardan çalıştırılmasından ötürü farklı klasörlerde Soru1.txt dosyaları oluştuğuna dikkat edin. FILE * dosya; dosya = fopen("soru1.txt", "w"); fprintf(dosya, "Merhaba Dunya!"); fclose(dosya); 2) Random Numbers to File Receive an unsigned integer N as input. Generate N random numbers and store them in a file. Put newline character between each generated number. 2) Dosyaya Rastgele Sayılar. Giriş olarak N işaretsiz tamsayısını alın. N adet rastgele sayı üretin ve bu sayıları bir dosyaya kaydedin. Her iki satır arasına yeni satır karakteri de koyun. #include <time.h> unsigned int n, N; fptr = fopen("random.txt", "w");
srand(time(null)); printf("enter N:"); scanf("%u", &N); n = 0; while(n<n) fprintf(fptr, "%d\n", rand()); n++; 3) Average of Random Numbers from File Receive file name as string. Open the file using the received string. Display count of numbers in the file. Find average of the numbers. 3) Dosyadan Rastgele Sayıların Ortalaması Dosya adını string ini girdi olarak alın. Dosyayı açın. Dosyada kaç adet sayı olduğunu ve sayıların ortalamasını çıktı olarak gösterin. char filename[filename_max]=""; unsigned int count=0, value; double total=0; printf("enter filename: "); scanf("%s", filename); fptr = fopen(filename, "r"); while(fscanf(fptr, "%u", &value)==1)
count++; total += value; printf("count: %u\naverage: %lf\n", count, total/count); 4) Word Count Receive file name as string and open the file. Display word count of the file. 4) Kelime Sayısı Dosya adı string ini girdi olarak alın. Dosyadaki kelime sayısını çıktı olarak gösterin. #define WORD_MAX 64 char filename[filename_max]=""; char word[word_max]=""; unsigned int count=0; printf("enter filename: "); scanf("%s", filename); fptr = fopen(filename, "r"); while(fscanf(fptr, "%s", word)==1) count++; printf("%u words.\n", count); 5) Monthly Income and Outcome A person keeps tracks of his/her income and outcome in txt files.
Data is entered per line as follows: dd.mm.yyy<tab>amount<tab>description string<newline>. You can download person s files from egecprogramlama.yolasite.com Write a program that receives filename and opens the expenses file. Compute income, outcome and balance and append to the end of file. 5) Aylık Gelir Gider Bir kişi, aylık gelir giderlerini txt dosyalarında takip etmektedir. Bilgiler, bir satırda bir bilgi olacak şekilde şöyle girilmektedir: gg.aa.yyyy<tab>miktar<tab>açıklama string i.<newline>. Kişinin dosyalarını egecprogramlama.yolasite.com dan indirebilirsin. Dosya adını girdi olarak alan ve dosyayı açan programı yazın. Geliri, gideri ve aylık neti hesaplayın ve dosyanın sonuna ekleyin. #define WORD_MAX 64 char filename[filename_max]="", date[word_max]="", desc[word_max]=""; double amount, income = 0, outcome = 0, balance; printf("enter filename: "); scanf("%s", filename); fptr = fopen(filename, "a+"); while(fscanf(fptr, "%s %lf %s", date, &amount, desc)==3) if(amount >= 0) income += amount; else outcome += amount; balance = income + outcome; fprintf(fptr, "\n\n income:\t%lf\noutcome:\t%lf\nbalance:\t%lf\n", income, outcome, balance); 6) Multiplication Table
Generate multiplication table save to file. 6) Çarpım Tablosu Çarpım tablosunu oluşturun ve dosyaya kaydedin. int i, j; fptr = fopen("multiplication.txt", "w"); if(fptr==null) for(i=1; i<=10; i++) for(j=1; j<=10; j++) fprintf(fptr, "%2d x %2d = %3d\n", i, j, i*j); fprintf(fptr, "\n\n"); 7) Calendar Receive an unsigned integer Y as input, which is year of calendar that will be generated. Generate the calendar and save to file, one date per line, double blank line between months. A leap year has 366 days and is evenly divisible by 4 but not divisible by 100, or divisible by 400. 7) Takvim. Takvimi oluşturulacak yılı, işaretsiz tamsayı tipinde girdi olarak alın. Bir satırda bir gün olacak şekilde takvimi oluşturun, aylar arasında iki tane boş satır bırakın. Artık yıllar 366 gündür. 4 e tam bölünen ve 100 e tam bölünmeyen veya 400 e tam bölünen yıllar, artık yıldır. #include <string.h> const char months[][16] = "Ocak ", "Subat ", "Mart ",
"Nisan ", "Mayis ", "Haziran", "Temmuz ", "Agustos", "Eylul ", "Ekim ", "Kasim ", "Aralik " ; const int days[] = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; int isleap(unsigned int year) if((year%4==0 && year%100!=0) year%400==0) return 1; else char year[8] = "", filename[32] = ""; unsigned int d, m, Y; printf("enter year: "); scanf("%s", year); Y = atoi(year); strcpy(filename, year); strcat(filename, ".txt"); FILE * fptr = fopen(filename, "w"); for(m=0; m<12; m++) for(d=1; d<=days[m]; d++) fprintf(fptr, "%2d %s %d\t\n", d, months[m], Y); if(m==2 && isleap(y)) fprintf(fptr, "%2d %s %d\t\n", d, months[m], Y); fprintf(fptr, "\n\n");