read file in C and split string [duplicate] - c

This question already has answers here:
Split string with delimiters in C
(25 answers)
Closed 9 years ago.
I have a program that read File in C. Now I want to put the strings divide by space into an array. How do I do it?
#include <stdio.h>
int main()
{
char line[30];
char names[100][20];
int sizes[100];
int i = 0;
FILE *fp;
fp = fopen("in.txt", "rt");
if(fp == NULL)
{
printf("cannot open file\n");
return 0;
}
while(fgets(line, sizeof(line), fp) != NULL)
{
printf(line);
i++;
}
fclose(fp);
return 0;
}

Have a look at the function strtok or strtok_r
http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok

Related

Read a file line by line and put the data in a struct array in c

I'm trying to read a text file in C. And I want to put the info in a struct.
I think I'm reading the file correctly but when I have to print the info, all the data is filled by the last line of the text.
Do you know why?
#include <stdio.h>
#define MAXCHAR 1000
int main() {
struct lumi {
char *domini;
char *disponible;
};
struct lumi registre[256];
int i = 0;
FILE *fp;
char str[MAXCHAR];
char* filename = "fitxer.txt";
fp = fopen(filename, "r");
if (fp == NULL){
printf("Could not open file %s",filename);
return 1;
}
while (fgets(str, MAXCHAR, fp) != NULL){
registre[i].domini = str;
registre[i].disponible = "offline";
i = i+1;
}
printf("%s", registre[0].domini);
printf("%s", registre[1].domini);
printf("%s", registre[2].domini);
printf("%s", registre[3].domini);
fclose(fp);
return 0;
}
The .txt I tried is
Hi
My
Name
Is

How to get string of unknown length in C? [duplicate]

This question already has answers here:
How can I read an input string of unknown length?
(11 answers)
Closed 3 years ago.
I have been trying to learn C, and was wondering: how would one get a string with an unknown length in C? I have found some results, but am not sure how to apply them.
If you're ok with extensions to the Standard, try POSIX getline().
Example from documentation:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(1);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (ferror(fp)) {
/* handle error */
}
free(line);
fclose(fp);
return 0;
}

How to read from files in argv or stdin if none are given? [duplicate]

This question already has answers here:
Read from file or stdin
(6 answers)
Closed 2 years ago.
I have a program that calculates a lottery tickets (this tickets are in a file.txt), and writes the winners tickets in another file. I have a subfunction called evaluate_tickets(file, lottery_numers, winner....)
In shell I write: ./program arg1 arg2... (arg1, arg2 are text files i.e. file.txt)
But now, I want to do ./program < file.txt. The problem is that I don't know how to send the parameter "file" of evaluate_tickets because I receive information by stdin.
Define a stream pointer FILE *fp; to read to input file:
If you want the input to be read from a file, use fp = fopen(filename, "r"); to open the file and close the stream after processing with fclose(fp);.
If you want the input to be read from standard input, just assign fp = stdin; instead of using fopen().
Here is a short example:
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp;
int c, lines;
if (argc > 1) {
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open %s\n", argv[1]);
return 1;
}
} else {
fp = stdin; /* read from standard input if no argument on the command line */
}
lines = 0;
while ((c = getc(fp)) != EOF) {
lines += (c == '\n');
}
printf("%d lines\n", lines);
if (argc > 1) {
fclose(fp);
}
return 0;
}
Here is the same example with a cleaner approach, passing stdin or an open FILE pointer to an ad hoc function. Note how it handles all command line arguments:
#include <stdio.h>
void count_lines(FILE *fp, const char *name) {
int c, lines = 0;
while ((c = getc(fp)) != EOF) {
lines += (c == '\n');
}
printf("%s: %d lines\n", name, lines);
}
int main(int argc, char *argv[]) {
FILE *fp;
if (argc > 1) {
for (int i = 1; i < argc; i++) {
fp = fopen(argv[i], "r");
if (fp == NULL) {
fprintf(stderr, "cannot open %s\n", argv[i]);
return 1;
}
count_lines(fp, argv[i]);
fclose(fp);
}
} else {
/* read from standard input if no argument on the command line */
count_lines(stdin, "<stdin>");
}
return 0;
}

Meaning of "open_file:" in c? [duplicate]

This question already has answers here:
What does the colon signify in a C function?
(3 answers)
Closed 5 years ago.
I was going through some code demonstrating error handling in file operations.
I can't understand what does open_file: mean and what does the colon after "open_file" signify ?
#include <stdio.h>
main()
{
char *filename;
FILE *fp1, *fp2;
int i, number;
fp1 = fopen("TEST", "w");
for(i = 10; i <= 100; i += 10)
putw(i, fp1);
fclose(fp1);
printf("\nInput filename\n");
open_file: //What does this mean ?
scanf("%s", filename);
if((fp2 = fopen(filename,"r")) == NULL)
{
printf("Cannot open the file.\n");
printf("Type filename again.\n\n");
goto open_file;
}
elsefor(i = 1; i <= 20; i++)
{ number = getw(fp2);
if(feof(fp2))
{
printf("\nRan out of data.\n");
break;
}
else
printf("%d\n", number);
}
fclose(fp2);
}
It's a goto label, used 7 lines below. It could be called sandeep: for all the compiler cares.
It's a label to which the goto a few lines later jumps..

Get number line of file text in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do get number line of file text on c ?. Help me .
Get sum number line.
I want read a file text.
EX:
for( line = 0; line < sumline; line ++) {
printf("char in line");
}
In case i understood the question :
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
char * line;
size_t len = 0;
ssize_t read;
int lines = 0;
fp = fopen("input.txt", "r");
if( fp != NULL ){
while ((read = getline(&line, &len, fp)) != -1){
lines ++;
printf("%s\n", line);
}
fclose(fp);
}
printf("number of lines : %d\n", lines);
}
to count how many lines in your file
Try this:
`int lines = 0;
while ((read = getline(&line, &len, fp)) != -1) {
lines++;
}
cout << lines << endl;`
You can use the following function to get the number of lines inside a file.
#include <stdio.h>
// get the number of lines inside file
int getLineCnt(char *pcFileName) {
FILE *fp;
int lines=0;
fp = fopen(pcFileName, "r");
if(fp == NULL) { return -1; }
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp, "%*c"))) {
++lines;
}
io_fclose(fp);
return lines; ///\ retval number of lines
}

Resources