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

Reads given file character by character and count them and lines in C

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

Writing a program that reads a given file character by character, and prints them on the screen, followed by the number of lines and characters. The name of the file will be read from the command line, as follows:
ProgramName filename
You are supposed to use the skeleton file ReadFile.c
After adding the missing codes, execute your program with the text file failure_to_communicate.txt given as the command line argument. Does your program print all the contents of the file? Are the values displayed for number of lines and characters correct? If not, correct the code appropriately so that your program prints all of the file contents, and the values displayed for number of lines and characters are correct.
Test: You can test your code by checking if the contents of the failure_to_communicate.txt are properly displayed attached bellow, followed by the number of lines and characters, which are 9 and 594, respectively.

failure_to_communicate.txt

 

File and Char operating
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
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, const char * argv[]) {
const char *fileName;
FILE *fp1;
int numLines = 0;
int numChars = 0;
int numChars1 = 0;
int numChars2 = 0;
    // Your code starts here
int myvariable;
 
if (argc != 2)
{
printf ("Filename is not given please try again");
return 0;
}
  
fileName = argv [1];
 
fp1 = fopen(fileName,"r");
if (fp1 != NULL) {
char c;
while ((c=fgetc(fp1))!=EOF) {
putchar(c);
   ++numChars;
   if (c != '\n') { ++numChars1; }
   if (c != ' ' && c != '\n') { ++numChars2; }
   if (c == '\n') { ++numLines; }
  
   }
   if (numChars > 0) {
++numLines;
   }
int count;
 
       printf ("\n------------------\n");
printf("\nNumber of lines:%d\nNumber of characters:%d\nNumber of characters(without enter):%d\nNumber of characters(without enter and space):%d\n", numLines, numChars, numChars1, numChars2);
fclose(fp1);
}
else {
printf("File: %s could not be opened\n", fileName);
}
return EXIT_SUCCESS;
}

 

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