I need to get lines from a text file. I already know that the lines won't be longer than 70 chars.
I have an idea about how to do it, but I'm looking a standard solution.
Maybe something like this ?
char line[MAXLEN];
while(fgets(line, sizeof(line), fp)) {
/* Do something with line. */
}
Don't forget that if you're reading in a file you need to have a file pointer and indicate what you want to do with the file. i.e. r -> read, w-> write. So it looks like you want to read the file.
So.....
Usage: gcc read.c -o read
"read input.txt"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[] ){
FILE *fp;
char buffer[70];
fp = fopen(argv[1], "r");
while(fgets(buffer,70,fp) != NULL){
puts(buffer);
}
fclose(fp);
}
This takes in the file input.txt from the command line, puts it in the char buffer, prints it, and repeats until end of file.
Cheers
Related
I'm trying to read a txt file containing strings of 1s and 0s and print it out in the manner below. I tried my code a couple of months ago and it worked fine in reading the text file. Now when I tried it, it outputs something really strange. Also I tried changing the directory of the file to a non-existant file but it still outputs the same thing when it should've quit the program immediately. Please help!
The content of txt file:-
10000001
01110111
01111111
01111010
01111010
01110111
Expected output:-
data_in<=24'b10000001;
#10000;
Real output:-
data_in<=24'b(some weird symbol that changes everytime I recompile);
#10000;
My code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int i, j;
j = 0;
char words[50];
FILE *fp;
fp = fopen (argv[1], "r");
if (fp == NULL) {
printf ("Can't open file\n");
}
while (feof (fp) == 0) {
fscanf (fp, "%s", words);
printf ("data_in<=24'b%s\n", words);
printf ("#10000\n");
}
fclose (fp);
system ("PAUSE");
return 0;
}
The input argument is the following:-
"C:\Users\Beanz\Documents\MATLAB\football frame\frame1.txt"
Read each line one by one with getline(3) -if available- or with fgets (you'll then need a large enough line buffer, at least 256 bytes), then parse each line buffer appropriately, using sscanf (the %n might be useful, and you should test the scanned item count result of sscanf) or other functions (e.g. strtok, strtol, etc...)
Remember that 'feof()' is only set AFTER trying to read PAST the end of the file, not when at the end of the file.
So the final iteration through the loop will try to read/process data that contains trash or prior contents.
Always check the returned value from 'fscanf()' before trying to use the associated data.
strongly suggest
eliminate the call to feof() and use the fscanf() to control the loop
I have an issue with as assignment regarding files.
Here is the assignment:
I am asked to write a code for a program that adds to each line in a text file, the number of that line. for example if the original file was:
Hi my name is Oria
I study programming
I love dogs
I use stackoverflow
It will be changed to:
1 Hi my name is Oria
2 I study programming
3 I love dogs
4 I use stackoverflow
But I don't know how to skip a line. After I've written the first number, how do I advance the *file pointer to be the first character of the next line?
This can be done with the help of writing it into another file.
Read the each line of a input file using fgets and start the loop count, then write to output file with count and data.
#include <stdio.h>
int main()
{
FILE *src, *dest;
char buf[64];
int i = 0;
src = fopen("in.txt", "r");
dest = fopen("out.txt", "w");
while(fgets(buf, 64, src) != NULL){
i++;
fprintf(dest, "%d %s", i,buf);
}
fclose(src);
fclose(dest);
return 0;
}
Use getline(3) to read lines in a loop. Within the loop, you can skip lines at will.
while (1) {
....
getline();
if (...)
continue;
}
Trying to learn C. Want to read the first line of a text file, my code is:
#include <stdio.h>
int main()
{
FILE *in = fopen("test.txt", "rt");
// read the first line from the file
char buffer[100];
fgets(buffer, 20, in);
printf("first line of \"test.txt\": %s\n", buffer);
fclose(in);
return 0;
}
I'm doing this in xCode. I get a exc bad access error.
test.txt definitely exists. It has one line that says "this is a text file"
try this after fopen() call:
if(in == NULL){
printf("Can't read teste.txt because: %s.\n", strerror(errno));
return 1;
}
and add the headers:
#include <errno.h>
#include <string.h>
The code looks fine, so my guess is that the program is not run in the same working directory as the file. Try placing the file in, say, /tmp/test.txt and use absolute path in fopen.
You don't check if the FILE is NULL. It may not be opened for a several reasons.
Is there any way for me to run a system command such as date through my C program and pipe the output to a char *date so I can use it later? I've been trying to use the "system" command but doing system("date"); immediately prints out the date output to stdout. I want to grab this data using system or exec within my program. Any suggestions would be appreciated...
Thanks!
Take a look at popen(). You open a FILE pointer with it, like so:
#include <stdio.h>
FILE *f = popen("date", "r");
And then you can use fread() or fscanf() to read from f into your buffer of choice.
You need the function popen declared in stdio.h. Then you can read the command's output like a file.
You probably want popen()
It sets everything up for you and does what you want
You may use the following code snippet as a reference.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char *command="date";
char line[256];
if ((fp = popen(command, "r")) == NULL) {
perror("popen failed");
return -1;
}
while (fgets(line, sizeof(line), fp))
printf("%s", line);
pclose(fp);
return 0;
}
I want to read a text file line by line, but I'm not interested in the white lines. What nice way is there of skipping the blank lines? I know I could read a line, check if it's blank and free it if it is, and so on until I reach a good line, but I'm wondering if there's some other way to do it.
I think your method is good enough. Technically you should even check if it's only spaces :-) Note that if you are using fscanf (quite used in homework problems), white line skipping is "Included in the price" :-) AND you don't have to fight against "this line is bigger than my buffer, what should I do?"
The general concept is fine ... you read in line by line and check to see if it has a non-whitespace character. A fairly optimum way of checking for it is to use strspn ... for example:
#include <stdio.h>
#include <string.h>
int is_blank_line(const char *line) {
const char accept[]=" \t\r\n"; /* white space characters (fgets stores \n) */
return (strspn(line, accept) == strlen(line));
}
int main(int argc, char *argv[]) {
char line[256]; /* assuming no line is longer than 256 bytes */
FILE *fp;
if ( argc < 2 ) {
fprintf(stderr, "Need a file name\n");
return -1;
}
fp = fopen(argv[1], "r");
if ( !fp ) {
perror(argv[1]);
return -1;
}
while (!feof(fp)) {
fgets(line, sizeof(line), fp);
if (is_blank_line(line)) {
continue;
}
printf("%s", line);
}
return 0;
}
If reading line by line use a simple check of '\n' (compiler will take care even if your real OS newline is \r\n).
If using fread to read whole file use strtok or strtok_r to split lines using sep='\n', empty lines will chopped out automatically.