Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

File Operations in C

Posted on January 24, 2018March 24, 2018 by Yılmaz Cemalettin

Task1

Write a program that joins the two sorted files to an output file such that the resulting file is also sorted. Your program should read the names of the files to be joined from the command line, as well as the name of the file which will store the joined content, as shown below:

ProgramName filename1 filename2 filenameOut

The files that your program will join contain floating point numbers sorted in increasing order. Your program should read from the input files and write to the output file appropriately so that the resulting file will also be sorted in increasing order.

In case one of the input files are not sorted in increasing order, your program should complain and exit, displaying an appropriate message.

You can test your code with different data files that are supplied.

Task2

Write a program that reads a given file containing floating point numbers and appends a summary line to the end of the file showing the count of numbers, the maximum number, the minimum number and a flag showing whether the file is sorted or not.
For data1.txt, the line will be like:
# 4 6.229683 0.586399 increasing
For data2_us.txt, the line will be like:
# 7 12.158343 0.279828 unsorted
The program should check whether last line of the file starts with the ‘#’ sign and update the line, in case it exists. Thus, after running the program with the same input file multiple times, there should exist only one summary line at the end of the file that starts with the ‘#’ sign.

Code1

Task1
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, const char * argv[]) {
const char *fileName , *fileName2 , *fileName3;
FILE *fptr , *fptr2 ,*fptr3 ;
int i = 0,k = 0,k1 = 0,q = 0;
int x, y;
float arr[20000];
float t0, temp;
 
 
if (argc != 4)
{
printf ("3 Filename is not given please try again");
printf("Usage: %s filename\n", argv[0]);
return EXIT_FAILURE;
}
fileName = argv [1];
fileName2 = argv [2];
fileName3 = argv [3];
 
fptr = fopen(fileName,"r");
  if(fptr == NULL){
    printf("Error!! Cannot open file: %s \n", fileName );
    return EXIT_FAILURE;}
  else
    {
      printf("First file opened successfully : %s \n", fileName);
      while(!feof(fptr))
    {  
      if(1 != fscanf(fptr, "%f" ,&arr[i]))
  {
  break;
  }
      printf("%f\n",arr[i]);      
     ++i;
    }
    }
q=i;
fptr2 = fopen(fileName2,"r");
  if(fptr2 == NULL){
   printf("Error!! Cannot open file: %s \n", fileName2 );
    return EXIT_FAILURE;}
  else
    {
      printf("Second file opened successfully: %s \n", fileName2);
      while(!feof(fptr2))
    {  
      if(1 != fscanf(fptr2, "%f" ,&arr[i])){
  break;
  };
      printf("%f\n",arr[i]);      
     ++i;
    }
    }
for(x=0; x<q-1; x++)
    {
if(arr[x]>arr[x+1])
k++;
}
for(x=q+1; x<i-1; x++)
    {
if(arr[x]>arr[x+1])
k1++;
 
}
if(k!=0){
printf("First file is not sorted Filename:  %s \n", fileName);
printf("Program will sort this array\n");
}
if(k1!=0){
printf("Second file is not sorted Filename:  %s \n", fileName2);
printf("Program will sort this array\n");
}
for(x=0; x<i; x++)
    {
        for(y=x+1; y<i; y++)
        {
          
            if(arr[y] < arr[x])
            {
                temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
            }
        }
    }
 
 
   fptr3 = fopen (fileName3, "w+");
  
    for(x=0; x<i; x++)
    {
        fprintf(fptr3,"%f\n", arr[x]);
    }
   fclose(fptr3);
fclose(fptr2);
fclose(fptr);
printf("\nElements of array in sorted check this file: %s ",fileName3);
  return 0;
}

 

Code2

Task2
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, const char * argv[]) {
const char *fileName ;
FILE *fptr, *freplace ;
int i = 0;
int x, k = 0, q = 0,y,z;
float arr[20000];
float t0, temp;
int c;
char s[100];
float max = arr[0];
float min = arr[0];
fileName = argv[1];
if (argc =! 2) {
printf("Usage: %s filename\n", argv[0]);
return EXIT_FAILURE;
}
 
 
fptr = fopen(fileName,"r+");
if(fptr == NULL){
printf("Error!! Cannot open file: %s \n", fileName );
return 1;
}
else {
printf("File opened successfully\n");
while((c = getc(fptr)) != '#' && c != EOF) {  
ungetc(c, fptr);
if(1 != fscanf(fptr, "%f" ,&arr[i])){
q=1;
}
++i;
 
}
}
if (c == '#' || q == 1)
{i=i-1; }
for(x=0;x<=i-2;x++) {
if(arr[x]>arr[x+1])
k++;
}
if (k==0)
{
strcat(s, "Sorted Increasing");
}
else {
strcat(s, "Unsorted");
}
min = arr[0];
for (y = 0; y < i; y++)
    {
      if (arr[y] > max)
        {
          max = arr[y];
        }
      else if (arr[y] < min)
        {
          min = arr[y];
        }
    }
 
if (c == '#' || q == 1)
{
freplace = fopen("reply.txt", "w");
for (z=0; z<=i-1; z++)
{
fprintf(freplace, "%f\n",arr[z]);
}
fprintf(freplace, "#Maximum value is:%f Minimum value is:%f Count of numbers:%i Numbers are %s",max,min,i,s);
fclose(fptr);
fclose(freplace);
remove(fileName);
rename("reply.txt", fileName);
}
else{
   fprintf(fptr, "\n#Maximum value is:%f Minimum value is:%f Count of numbers:%i Numbers are %s",max,min,i,s);
fclose(fptr);
 
}
return 0;
}

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme