I have this program which deletes from an .txt file all the words, which start and end with the same symbol. In my opinion it should work, but somehow it doesn't stop when EOF is reached and ir prints me some strange chinese symbols...
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 255
void search(char *symbolMass, FILE *duomFail, FILE *rezFail)
{
int i = 0, k =0, j =0, p = 0;
char symbol = 0;
char *rezMass;
char word[20];
rezMass = (char*)malloc(sizeof(char)*MAX);
while(simbolis != EOF)
{
printf("veikia");
symbol = symbolMass[i];
if (symbol != 32 && symbol != 10 && symbol != EOF)
{
word[j] = symbol;
i++;
j++;
}
else
{
word[j] = symbol;
i++;
if(word [0] == word[j - 1])
{
rezMass[k] = word[j];
k++;
}
else
{
for (p = 0; p <= j; p++, k++)
{
rezMass[k] = word[p];
}
}
j = 0;
}
}
for(i = 0; i <= k; i++)
symbolMass[i] = rezMass[i];
}
int main(int argc, char* argv[])
{
FILE *duom, *rez;
char *symbols;
symbols = (char*)malloc(sizeof(char)*MAX);
if (argc > 1)
{
duom = fopen (argv[1],"r");
rez =fopen (argv[2],"w");
if (duom != NULL)
{
while (symbols != NULL)
{
fgets(symbols, MAX, duom);
search(symbols, duom, rez);
fputs(symbols, rez);
}
fclose(duom);
}
else
{
printf("There is no file with name \"%s\"\n",argv[1]);
}
}
else
{
printf("The command has no arguments.\n");
}
fclose(rez);
free(simboliai);
return 0;
}
It works like this: it scans symbols and puts them into "word" till it reaches "space", "new line" or "EOF", then it checks if the word starts and ends with the same symbol, if yes, it prints only the "space", "new line" or "EOF", if not, then it prints the whole word.
Oh, and the code is wrote in C (usiing CodeBlocks if it matters).
That's because there is no EOF character in the buffer you pass to the search function. The buffer, symbolMass is a string and like all strings in C it's terminated by the special null-character '\0' (which incidentally happens to be the same as 0).
I suggest you change your loop to something like this
char symbol;
for (int i = 0; (symbol = symbolMass[i]) != '\0'; ++i)
{
...
}
Also, don't use "magic numbers" for characters, use the actual character literals instead, so instead of
if (symbol != 32 && symbol != 10 && symbol != EOF)
do
if (symbol != ' ' && symbol != '\n')
Related
I want to read a c file from another c program.
and print it line by line.
But I got some problem.
here is my code, the file to be read and the output i'm getting in terminal.
my Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sourcefilename[100];
char targetfilename[100];
int counter = 0;
int lower_limit = 10;
char *line = NULL;
char *temp = NULL;
int is_multilinecomment = 0;
FILE *source = fopen("hello.c", "r");
FILE *target = fopen("newcode.c", "w");
char ch = fgetc(source);
while (ch != EOF)
{
// printf("%c", ch);
if (ch == '\n')
{
counter = 0;
printf("%s\n", line);
free(line);
line = NULL;
}
else
{
temp = (char *)realloc(line, counter * sizeof(char));
if (!temp)
{
free(line);
line = NULL;
}
line = temp;
line[counter] = ch;
counter++;
// printf("%s", line);
}
// printf("helo");
ch = fgetc(source);
}
return 0;
}
hello.c
I'm trying to read this file
#include <stdio.h>
#include <string.h>
// this is a single line comment
int main()
{
char var[500];
printf("Enter a name of a variable : ");
scanf("%s", var);
if (!((var[0] >= 'a' && var[0] <= 'z') || (var[0] >= 'A' && var[0] <= 'Z') || var[0] == '_'))
{
printf("%s is not valid variable.\n", var);
return 0;
}
// this is another single line comment
for (int i = 1; i < strlen(var); i++)
{
if (!((var[i] >= 'a' && var[i] <= 'z') || (var[i] >= 'A' && var[i] <= 'Z') || var[i] == '_' || (var[i] >= '0' && var[i] <= '9')))
{
printf("%s is not valid variable.\n", var);
return 0;
}
}
/*
this is a multi line
comment */
printf("%s is valid variable.\n", var);
return 0;
}
output i'm getting
#include <stdio.h>\Progr�
#include <string.h>
// this is a single line comment`�
int main()�
{�
char var[500];e line/
printf("Enter a name of a variable : ");{~
scanf("%s", var);ame/
if (!((var[0] >= 'a' && var[0] <= 'z') || (var[0] >= 'A' && var[0] <= 'Z') || var[0] == '_'))�
{
printf("%s is not valid variable.\n", var);
return 0;s is nok�_z~
}
// this is another single line comment
for (int i = 1; i < strlen(var); i++)
{
if (!((var[i] >= 'a' && var[i] <= 'z') || (var[i] >= 'A' && var[i] <= 'Z') || var[i] == '_' || (var[i] >= '0' && var[i] <= '9')))
_�
you can notice here i'm getting some unwanted characters at the end of each line.
and also last part of the hello.c is ignored.
please help !!!
You have multiple issues in your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sourcefilename[100];
char targetfilename[100];
int counter = 0;
int lower_limit = 10;
char *line = NULL;
char *temp = NULL;
int is_multilinecomment = 0;
FILE *source = fopen("hello.c", "r");
FILE *target = fopen("newcode.c", "w");
char ch = fgetc(source);
Function fgetc returns an int. This is required to detect EOF. Do not use char.
while (ch != EOF)
See Why is “while ( !feof (file) )” always wrong? for details
{
// printf("%c", ch);
if (ch == '\n')
{
counter = 0;
printf("%s\n", line);
If you hit an empty line, you do not have any memory allocated for line and it contains NULL, causing undefined behaviour.
In any case you do not have a terminating 0 byte in your line buffer. This means it is not a valid string and passing that character array to printf again causes undefined behaviour.
free(line);
line = NULL;
}
else
{
temp = (char *)realloc(line, counter * sizeof(char));
You start with counter=0 for each line. You allocate 0 bytes for first character. Instead of allocating memory for your string + 1 byte for terminating \0 byte, you allocate 2 bytes less.
if (!temp)
{
free(line);
line = NULL;
}
line = temp;
line[counter] = ch;
If condition (!temp) was true above, you still assign temp to line and dereference it, causing undefined behaviour.
counter++;
// printf("%s", line);
}
// printf("helo");
ch = fgetc(source);
}
return 0;
}
Some more issues, not related with weird characters:
You do not write your output to target file.
You do not close your files.
A proper signature for main should be int main (void). Empty parameter list in function definition should not be used nowadays.
Most of your variables in main are unused.
Not all included headers are actually required.
A palindrome is a word that reads the same from left to right and from right to left.
I wrote a program that finds palindromes from a console.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100
int main() {
int i = 0, c;
int left, right;
char string[SIZE];
while (EOF != (c = getchar()) || (c = getchar()) != '\n') {
if (isspace(c) != 0) {
if (i > 0) {
left = 0;
right = i - 1;
while (right > left) {
if (string[left] != string[right]) {
i = 0;
break;
}
++left;
--right;
}
if (left >= right) {
while (i > 0)
printf("%c", string[--i]);
printf("%c", c);
}
i = 0;
}
if (c == '\n')
break;
}
else {
string[i++] = c;
}
}
}
For example, we enter the words: dad sad mum. She outputs: dad mum. But if we write dad sad or dad mum sad. The output will be: dad mum.
That is, an extra space is printed when the last word we read is not a palindrome. How can you get around this situation?
Code is convoluted
First read input properly and form a string.
for (i = 0; i < SIZE - 1; i++) [
int ch = getchar();
if (ch == EOF || ch == '\n') {
break;
}
string[i++] = (char) ch;
}
string[i] = 0;
Then process the string in string[]. Only print spaces when needed.
const char *separator = "";
int i = 0;
while (string[i]) {
// Beginning of a word?
if (!isspace(string[i])) {
int start = i;
int end = i;
while (!isspace(string[end+1]) && string[end+1]) {
end++;
}
// At this point, start indexes the 1st char of the word
// and end indexes the last char of the word
// Now find if a palindrome
while (start <= end && string[start] == string[end]) {
start++;
end--;
}
// Found a palindrome?
if (start > end) {
fputs(separator, stdout);
separator = " "; // print a space _next_ time
while (!isspace(string[i]) && string[i]) {
fputc(string[i++], stdout);
}
} else {
i = end + 1;
}
} else {
i++;
}
}
fputc('\n', stdout);
Life is easier if you just read the string all at once, then process the string.
char s[1000];
fgets( s, sizeof(s), stdin );
char * p = strchr( s, '\n' );
if (p) *p = '\0';
If you wanted to read one character at a time you should read once, test twice:
int c;
while ( ((c = getchar()) != '\n') and (c != EOF) )
But trying to compute the palindrome-ness at the same time as reading seriously makes your algorithm waaaaay more complicated than it needs to be. Read a string first, then compute.
Now you can use integer indices from each end of the string. If you can get them to meet (or cross) then you’ve got a palindrome. Hint: put that in a function:
bool is_palindrome( const char * s )
{
int left = 0;
int right = strlen(s) - 1;
...
}
Snapshot
Introduction
Usual search engines receive a set of keywords and look for all the
documents that contain these keywords. The documents are listed in the
order of document significance. In this problem we consider the
significance of a document for a set of keywords is given by the
minimum number of words of the continuous piece of text that contains
all the searched keywords. For instance: consider the keywords “2008”
and “IEEEXtreme”, and the following two texts: “The registration for
the 2008 edition of IEEEXtreme is now open” and “IEEEXtreme 2008
edition is going to take place on March 8th 2008”. The significance of
the first text is 4, and of the second one is 2. If any of the given
words is not present in the text, the significance is zero.
Task
Please write a program that reads from the standard input a text in
which the words are separated only by spaces, and finds the
significance of text against the keywords given as the parameters to
your program.
Syntax
For the input text:
The registration for the 2008 edition of IEEEXtreme is now open
your program executed as:
> snapshot 2008 IEEEXtreme
should write 4 on the standard output. Note: if not all
the words are found, the program should return 0.
#include <stdio.h>
#include <string.h>
int compare(char *x, char *z) {
int a = 0;
if (strlen(x) == strlen(z)) {
while (a < strlen(x)) {
if (x[a] == z[a])
a++;
else
return 0;
}
return 1;
}
}
int verify(int q, int n, char *v) {
static int flag2 = 0;
static int error = 0;
if ((v[0] == '#') && (v[1] == '#') && (v[2] == '#') && (v[3] == '#')
&& (v[4] == '#')) {
flag2 = 1;
} else {
error++;
}
if ((q = n - 1) && flag2 == 1 && error == 0)
return 1;
else
return 0;
}
int main(int argc, char *argv[]) {
char text[1000];
char word[30];
FILE *fp = fopen("filename", "r");
int i = 0, j = 0, k = 0, y = 1, w = 1, t = 1, flag = 0, signifiancia = 0,
sucesso = 0;
while (feof(fp))
text[i++] = fgetc(fp);
text[i] = '\0';
while (text[j] != '\0') {
if (text[j] == ' ') {
j++;
word[k] = '\0';
k = 0;
while (y < argc) {
compare(argv[y], word);
if (1) {
flag = 1;
argv[y] = "#####";
signifiancia++;
y++;
} else {
if (flag = 1)
signifiancia++;
y++;
}
}
} else {
word[k] = text[j];
j++;
k++;
}
while (w < argc) {
verify(t, argc, argv[w]);
t++;
if (1) {
sucesso++;
printf("%d", signifiancia);
}
}
}
if (sucesso == 0)
printf("0");
}
The error given is:
Segmentation fault (core dumped)
At least these problems:
Missing return
When if (strlen(x) == strlen(z)) is false, function does not return anything.
Yet calling code does not use the return value anyways.
Assignment rather than compare
if (flag = 1)
Too many loops
Code iterates once too often.
while (feof(fp))
text[i++] = fgetc(fp);
Infinite loop
Once while (w < argc) { loop is entered, it appears to iterate infinitely - likely leading to UB.
Failure to prevent buffer over-runs
I have a program where my code uses a goto statement, and I want to get rid of it in a nice way, but I can't seem to find a solution. If goto is the best way, then please let me know. Here is a summary of the code:
//Counts how many times every word appears in a file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMWORDS 1000
#define WORDLEN 50
typedef struct
{
char word[WORDLEN + 1];
int num;
} Appearance;
int main(int argc, char *argv[]) {
FILE *readfile;
Appearance *appearlist[NUMWORDS] = {NULL};
char word[WORDLEN + 1];
int i;
//Get a valid filename and open the file, store pointer into readfile
...
char c;
while (c != EOF) {
skip: //Annoying label
//Get a word from readfile, store into word
...
if (word[0] != '\0') {
for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
if (strcmp(appearlist[i] -> word, word) == 0) {
appearlist[i] -> num++;
goto skip; //Annoying goto
}
}
appearlist[i] = (Appearance *) malloc(sizeof(Appearance));
appearlist[i] -> num = 1;
strcpy(appearlist[i] -> word, word);
}
}
//Display results, free memory
...
return 0;
}
The problem is, I want to skip code that is outside of the loop I want to skip from. I would like to not create another variable only designed for this. If you want the full code, click on "Show code snippet."
//Counts how many times every word appears in a file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMWORDS 1000
#define WORDLEN 50
#define FILENAMELEN 50
typedef struct
{
char word[WORDLEN + 1];
int num;
} Appearance;
int main(int argc, char *argv[])
{
char filename[FILENAMELEN];
FILE *readfile;
Appearance *appearlist[NUMWORDS] = {NULL};
char word[WORDLEN + 1];
size_t ln;
int i;
if (argc == 2)
strncpy(filename, argv[1], sizeof(filename));
else {
printf("Enter a filename to count appearances from, or just press enter to quit: ");
fgets(filename, FILENAMELEN, stdin);
ln = strlen(filename) - 1;
if (filename[ln] == '\n')
filename[ln] = '\0';
}
while((readfile = fopen(filename, "r")) == NULL) {
if (filename[0] == '\0')
return 0;
printf("Invalid file! Please enter another filename, or just press enter to quit: ");
fgets(filename, FILENAMELEN, stdin);
ln = strlen(filename) - 1;
if (filename[ln] == '\n') filename[ln] = '\0';
}
char c;
while (c != EOF) {
skip:
for (i = 0; (c = getc(readfile)) != EOF && (isalnum(c) || c == '\''); i++) {
if (i >= WORDLEN) {
word[i] = '\0';
printf("\nWarning: word too long (over %d characters), trimming to: %s\n", WORDLEN, word);
while ((c = getc(readfile)) != EOF && (isalnum(c) || c == '\'')) ;
} else {
word[i] = tolower(c);
}
}
word[i] = '\0';
if (word[0] != '\0') {
for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
if (strcmp(appearlist[i] -> word, word) == 0) {
appearlist[i] -> num++;
goto skip;
}
}
appearlist[i] = (Appearance *) malloc(sizeof(Appearance));
appearlist[i] -> num = 1;
strcpy(appearlist[i] -> word, word);
}
}
for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
printf("Word: %s, Appearances: %d\n", appearlist[i] -> word, appearlist[i] -> num);
free(appearlist[i]);
}
return 0;
}
Using goto in this case is often considered acceptable.
Alternatives would be to set a variable so that you can continue in the outer loop after breaking from the inner one, or turning the whole segment that you want to escape from into a separate function, and returning from it instead of using goto.
I'm ignoring any other issues there may be with the code that aren't relevant to the question!
Put everything beginning with the 'if' statement into a separate method (let's call it "process" and replace the goto with return. Then the while-loop becomes:
while (c != EOF) {
//Get a word from readfile, store into word
...
process(...);
}
Sometimes using goto is a hint that code should use a helper function
static bool findword(Appearance *appearlist, size_t size, const char *word) {
for (size_t i = 0; i < size && appearlist[i]; i++) {
if (strcmp(appearlist[i]->word, word) == 0) {
appearlist[i]->num++;
return true;
}
}
return false;
}
while (c != EOF) {
//Get a word from readfile, store into word
...
if (word[0] != '\0') {
if (findword(appearlist, NUMWORDS, word)) {
continue;
}
appearlist[i] = (Appearance *) malloc(sizeof(Appearance));
appearlist[i] -> num = 1;
strcpy(appearlist[i] -> word, word);
}
}
I am creating a program that prints each line of a file one by one in reverse word order. i.e. "The big black moose" prints as "moose black big The".
However, in the code below, it does not print the last word of lines that do not have a delimiter before the line break. A delimiter in this case is defined as any whitespace character such as space or tab.
int main(int argc, char const *argv[]) {
if (argc != 2) return 0;
int i = 0, c;
int isD = 0, wasD = 1;
int count = 0;
FILE *file = fopen(argv[1], "r");
while ((c = getc(file)) != EOF) {
isD = c == ' ' || c == '\t' || c == '\n';
if (!isD) {
chars[i++] = c;
count++;
}
if (isD && !wasD) {
shiftInsert(i++, count);
count = 0;
}
wasD = isD;
}
fclose(file);
return 0;
}
int shiftInsert(int length, int shift) {
int word[shift+1], i;
printf("\n----------\nL:%d,S:%d\n", length, shift);
for (i = 0; i < shift; i++)
word[i] = chars[length-shift+i];
word[shift] = ' ';
for (i = 0; i < shift; i++)
printf("%c", word[i]);
for (i = length; i >= 0; i--)
chars[i+shift+1] = chars[i];
for (i = 0; i <= shift; i++)
chars[i] = word[i];
printf("|");
}
This happens, because you don't enter the loop when getc finds the end of the file. If wasD is false, you'll have one unprocessed word in the buffer.
You could treat EOF as whitespace and place the terminating condition at the end of the loop:
do {
c = getc(file);
isD = (c == ' ' || c == '\t' || c == '\n' || c == EOF);
// ...
} while (c != EOF);
This works, because you use the value of c only if it is not a delimiter. (The special value EOF is outside the valid range of (unsigned) chars and should not be inserted into strings or printed.)
stdout is not getting flushed because your last output didn't contain a newline...
Change this line
printf("|");
to
printf("|\n");