Compare commits
6 Commits
f4c0563a3a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a58f5db5a3 | |||
| b826126635 | |||
| 8020d45db7 | |||
| 9b4fd1e5c1 | |||
| 17abac161e | |||
| d10617f2d4 |
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# this is a comment
|
||||||
|
|
||||||
|
# can we ignore any file without an extension?
|
||||||
|
|
||||||
|
copy
|
||||||
|
out.txt
|
||||||
32
c_programming/files/copy.c
Normal file
32
c_programming/files/copy.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE *input = fopen("in.txt", "r");
|
||||||
|
if (input == NULL)
|
||||||
|
{
|
||||||
|
perror("Failed to open in.txt");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
FILE *output = fopen("out.txt", "w");
|
||||||
|
if (output == NULL)
|
||||||
|
{
|
||||||
|
perror("Failed to open out.txt");
|
||||||
|
fclose(input);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
char c;
|
||||||
|
while (fread(&c, sizeof(char), 1, input) != 0)
|
||||||
|
{
|
||||||
|
fwrite(&c, sizeof(char), 1, output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO:
|
||||||
|
+ clone: clone git repository and start working on copy.c
|
||||||
|
+ first: go through part 3 of cs50 week 4 memory section.pdf
|
||||||
|
+ make: get copy.c compiling with make
|
||||||
|
+ complete: fix all problems and ensure the demo program works as intended
|
||||||
|
+ wait: if I need to go work on another file, it will go in a wait: task.
|
||||||
|
*/
|
||||||
8
c_programming/files/files.txt
Normal file
8
c_programming/files/files.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
Reading and Writing Files in C
|
||||||
|
|
||||||
|
copy - copy one file to a new file, one letter at a time.
|
||||||
|
|
||||||
|
lines - read lines from a text file using fgets.
|
||||||
|
todo: lines_2 - remove the \n from the end of each line.
|
||||||
|
https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input
|
||||||
|
https://man.freebsd.org/cgi/man.cgi?query=strcspn&sektion=3
|
||||||
1
c_programming/files/in.txt
Normal file
1
c_programming/files/in.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
hi!
|
||||||
42
c_programming/files/lines.c
Normal file
42
c_programming/files/lines.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// A program that iterates one line at a time.
|
||||||
|
// uses fgets.
|
||||||
|
// https://manual.cs50.io/3/fgets
|
||||||
|
|
||||||
|
// read the lines from lines.txt and print them out.
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// char *fgets(char s[restrict .size], int size, FILE *restrict stream);
|
||||||
|
|
||||||
|
// reads in at most one less than size characters from stream and stores them
|
||||||
|
// into the buffer pointed to by s. Reading stops after an EOF or a newline.
|
||||||
|
// If a newline is read, it is stored into the buffer. A terminating null byte
|
||||||
|
// ('\0') is stored after the last character in the buffer.
|
||||||
|
|
||||||
|
// lines.c is based on the example at https://en.cppreference.com/c/io/fgets
|
||||||
|
|
||||||
|
|
||||||
|
#define SIZE 64
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE* infile = fopen("lines.txt", "r");
|
||||||
|
if (infile == NULL)
|
||||||
|
{
|
||||||
|
perror("Failed to open in.txt");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
char buf[SIZE];
|
||||||
|
|
||||||
|
while (fgets(buf, sizeof buf, infile) != NULL)
|
||||||
|
{
|
||||||
|
printf("%s", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (feof(infile))
|
||||||
|
{
|
||||||
|
puts("End of file reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
4
c_programming/files/lines.txt
Normal file
4
c_programming/files/lines.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Line one
|
||||||
|
Line two
|
||||||
|
|
||||||
|
Line four
|
||||||
1
git/ignore_compiled.txt
Normal file
1
git/ignore_compiled.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
To make git ignore compiled C programs, we put this in the git ignore.
|
||||||
7
git/start_ignoring_me.txt
Normal file
7
git/start_ignoring_me.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
To make git ignore things, we need a gitignore.
|
||||||
|
Here is how to get started with that.
|
||||||
|
|
||||||
|
A tool for creating gitignore things: https://www.toptal.com/developers/gitignore/
|
||||||
|
|
||||||
|
Of course, there is official documentation for git ignore: https://git-scm.com/docs/gitignore
|
||||||
|
|
||||||
Reference in New Issue
Block a user