I'm trying to read the first character of a file and whenever it's equal to '(' I should skip that line else get the first character from that line. I'm under a mac and I can make use of fgetln.
FILE *file = fopen("test.txt", "r");
char c;
while(fscanf(file, "%s", &c) != EOF) {
if (c != '(')
printf("%c", c);
}
That's my current code. I don't know how to skip lines, although I've tried to get the whole line and checked only the first char solving the skip problem. However this is not working I'm getting strange characters in my console instead of the ones inside test.txt. How should I do that?
The problem with using %s format specifier of fscanf is that is splits on spaces, not only on end-of-line characters. Moreover, reading it in a single-character buffer will nearly always produce undefined behavior.
There are several ways to solve this problem, using different APIs:
You could replace %s with %200[^\n], and passing a 201-character buffer instead of c,
Using fgets with a properly-sized buffer, and picking the initial character, or
Using a character-based API, and setting a "take next" flag each time that you see a '\n' character:
Here is how you can implement the third approach:
bool takeNext = true;
int ch;
while ((ch = fgetc(file)) != EOF) {
if (takeNext && ch != '(') {
printf("%c", ch);
}
takeNext = (ch == '\n');
}
Here is a slightly longer character-based approach, which conditions on whether the first character in a line is ( or not.
If it is (, then we consume everything up to and including the next newline without outputting.
If it not, then we do the same thing but we output the characters as we read them.
#include <stdio.h>
int main(){
FILE *file = fopen("test.txt", "r");
int c;
while((c = getc(file)) != -1) {
if (c == '(') {
// Skip until the next newline
do {
c = getc(file);
} while (c != -1 && c != '\n');
continue;
}
else {
putchar(c);
do {
c = getc(file);
putchar(c);
} while (c != -1 && c != '\n');
}
}
fclose(file);
}
Change c to string because fscanf reads string. See if the 1st character of c matches with (.
If it does not then print the line else skip the line.
FILE *file = fopen("test.txt", "r");
char c[100];
while(fscanf(file, "%s", c)) {
if (c[0] != '(')
printf("%s", c);
}
Use fgets to read whole lines. It is also safer than fscanf as it limits the reading to the buffer size.
To check if the first char is '(' you can refer to it directly:
if (buf[0]=='(')
or
if (*buf=='(')
Related
I am trying to read a text file using the FILE* type. Everything is fine, except with the fgetc() function.
In fact, I'd like to check how many '\n' characters are there in the file, but the function never returns that value even though there is one in the file I'm reading. Has someone ever experienced this situation?
The code i'm using is the following:
char ch;
while (ch!=EOF) {
ch = (char) fgetc(gl_file);
if(ch=='\n') newline_counter++;
}
newline_counter remains 0.
You don't initialize ch before checking its value and ch is char while fgetc returns int.
Rewrite to something like:
int ch;
while ((ch = fgetc(fp)) != EOF)
{
if(ch == '\n')
++newline_counter;
}
no need to cast returned value from fgetc. use getc instead of fgetc :
getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.
char ch = getc(fp);
do{
if (ch == '\n') ++newline_counter;
} while ((ch = getc(fp)) != EOF);
or you can use getline():
getline() reads an entire line from stream.
char *ch = NULL;
size_t len = 0;
while (getline(&ch, &len, file) != -1)
++line;
If *ch is set to NULL and *len is set 0 before the call, then getline() will allocate a buffer for storing the line.
I'm trying to print the chars (with fgetc) in a file one by a one with a while loop.
I'm using the latest Atom editor to write the code, and I compile with the GPP Compiler, by pressing F5 and the output is displayed in the xterm terminal.
int main(int argc, char const *argv[])
{
FILE* file = NULL;
file = fopen("text.txt", "r+");
int letter = 0;
if (file != NULL)
{
while(letter != EOF)
{
letter = fgetc(file);
printf("%c", letter);
}
I expected the output to be the text in my file, which it is, but at the end there's a question mark symbol.
What I understood after doing some research is that my fgetc function reads the EOF like a normal character and prints it, resulting in a question mark symbol at the end.
Thanks for your help !
... ending with a “?” symbol
doing
while(letter != EOF)
{
letter = fgetc(file);
printf("%c", letter);
}
you print letter before to check if it is EOF, so you (try to) print EOF which is not a character, producing the unexpected output
Example of a valid code :
while ((letter = fgetc(file)) != EOF)
putchar(letter); /* or printf("%c", letter); if you prefer */
I have been having the same issue and I found out that the return type for fgetc is an integer and it may be returning -1.
while((c = getc(file)) != -1)
{
if (c == ';')
{
//here I want to skip the line that starts with ;
//I don't want to read any more characters on this line
}
else
{
do
{
//Here I do my stuff
}while (c != -1 && c != '\n');//until end of file
}
}
Can I completely skip a line using getc if first character of line is a semicolon?
Your code contains a couple of references to -1. I suspect that you're assuming that EOF is -1. That's a common value, but it is simply required to be a negative value — any negative value that will fit in an int. Do not get into bad habits at the start of your career. Write EOF where you are checking for EOF (and don't write EOF where you are checking for -1).
int c;
while ((c = getc(file)) != EOF)
{
if (c == ';')
{
// Gobble the rest of the line, or up until EOF
while ((c = getc(file)) != EOF && c != '\n')
;
}
else
{
do
{
//Here I do my stuff
…
} while ((c = getc(file)) != EOF && c != '\n');
}
}
Note that getc() returns an int so c is declared as an int.
Let's assume that by "line" you mean a string of characters until you hit a designated end-of-line character (here assumed as \n, different systems use different characters or character sequences like \r\n). Then whether the current character c is in a semicolon-started line or not becomes a state information which you need to maintain across different iterations of the while-loop. For example:
bool is_new_line = true;
bool starts_with_semicolon = false;
int c;
while ((c = getc(file) != EOF) {
if (is_new_line) {
starts_with_semicolon = c == ';';
}
if (!starts_with_semicolon) {
// Process the character.
}
// If c is '\n', then next letter starts a new line.
is_new_line = c == '\n';
}
The code is just to illustrate the principle -- it's not tested or anything.
I'm trying to read in a file that has different types to read in (integers, chars). This is relatively simple, yet I'm confused on which method to use to read in these different values.
I'm using fgets to make sure the file isn't at the end, i.e;
char line[MAX_CHARS];
char ch;
FILE *infile = fopen("file.txt", "r");
const int MAX_CHARS = 50;
while(fgets(line, MAX_CHARS, infile) != NULL)
Given the input;
-Flight one
83, 34
X XX X
X X
-Flight two
....
I want to print the line that starts with a dash, send the integers to a method, and then print the X's and spaces. So, my code for this would be:
if(line[0] == '-')
{
printf("%s\n", line);
}
else if(2==sscanf(line, "%d%d", &long, &lat))
{
coordinates(long, lat);
}
I used scanf to try to read the X's and spaces, but it doesn't work at all. getchar() doesn't seem to work either, so should I start over and instead read each char individually instead of a char array?
EDIT: So I did as someone suggested, this is my updated code to read in spaces and X's, but it is clearly not reading right, as it's not going to the next line of X's.
else
{
while(line[++index] != '\0')
{
if(line[index] == ' ')
{
printf("%c", '.');
}
else if(line[index] == 'X')
{
printf("%c", '*');
}
}
}
For output;
*Flight one
.......*Flight two
scanf(line, " %c", &ch)
/* ^ */
The space before percent will cause scanf to ignore all whitespace characters (space, tab, newline) if present.
i.e. condition if(ch == ' ') will be always false.
In your question, this is not something that you like, so remove this whitespace.
EDIT
Also as suggested by Barak Manos
I think that for the second line you need to scan "%d, %d" instead of "%d%d".
I know I can get the first character of a line of standard input by using getchar(), but I only want the first character of each line. Is there a function I can use to get rid of the rest of the string entered into standard input (if it is more than one character)? if not, what methodology should I consider using to get rid of the rest of the standard input line?
char buf[100];
while(fgets(buf,sizeof(buf),stdin) != NULL)
{
if(strlen(buf)>0)
buf[1] = '\0';
printf("%s",buf);
}
Read the whole line using fgets() and just nul terminate it after the first character.
#include <stdio.h>
int main(void)
{
int ch;
size_t len;
for (len = 0; 1; ) {
ch = getc(stdin);
if (ch == EOF) break;
if (!len++) putc(ch, stdout); /* the first character on a line */
if (ch == '\n') len = 0; /* the line has ended */
}
return 0;
}
Please note that the first character on a line can actually be a '\n' !!!
// Get the character you need
char c = getchar();
// Skip the rest
int a;
while((a = getchar()) != '\n' && a != EOF);
If you know how many lines you'll have, you can put it in a loop.