The code reads a text file delimited by colons : and formatted as follows
1111:2222:3333
How would I store the values separated by colons : into separate variables ?
any help would be appreciated.
program code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int read_file();
int main()
{
read_file(); // calls function to read file
return 0;
}
// read text file function
int read_file()
{
char line[100];
char file_location[40];
FILE *p_file;
printf("Enter file location: ");
scanf("%s",file_location);
p_file =fopen(file_location, "r");
if(!p_file)
{
printf("\n File missing:");
return 0;
}
while(fgets(line,100,p_file)!=NULL)
{
printf("%s \n",line);
}
fclose(p_file);
return 0;
}
This will give you a hint :
Use strtok as you would do for reading a csv file
while(fgets(line,100,p_file) != NULL)
{
char *p = strtok(line, ":");
while(p)
{
printf("%s\n", p); //Store into array or variables
p=strtok(NULL, ":");
}
}
POW already gave you everything you need to know.
So, FWIW:
One of the things C coders do is to keep a library of simple utitlies. Whacking a string up using delimiters is one of those utilities.
Here is a very simple (no error checking) example:
char **split(char **r, char *w, const char *src, char *delim)
{
int i=0;
char *p=NULL;
w=strdup(src); // use w as the sacrificial string
for(p=strtok(w, delim); p; p=strtok(NULL, delim) )
{
r[i++]=p;
r[i]=NULL;
}
return r;
}
int main()
{
char test[164]={0x0};
char *w=NULL; // keep test whole; strtok() destroys its argument string
char *r[10]={NULL};
int i=0;
strcpy(test,"1:2:3:4:hi there:5");
split(r, w, test, ":\n"); // no \n wanted in last array element
while(r[i]) printf("value='%s'\n", r[i++]);
printf("w='%s' test is ok='%s'\n",
(w==NULL)? "NULL" : w, test);// test is still usable
free(w); // w is no longer needed
return 0;
}
Related
I have found some information about strcat and experimented with it but it doesn't work the way i expected for example :
char a = 'a', b = 'b';
strcat(a,b);
printf("%c", a);
this will produce an error "initialization of 'char' from 'char *' makes integer from pointer without a cast". Is there a way to unite chars until the wanted word is complete and store it in 1 variable? Or am i going completely wrong about this. The purpose of the code is to read an xml file and build a tree with the tags.
Any help is or advice is very much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int count = 0;
char c, word;
FILE *file = fopen("example.xml", "r");
if (file == NULL) {
return 0;
}
do {
c = fgetc(file);
if (c == '<') {
count = 1;
}
if (c == '>') {
count = 0;
printf(">");
}
if (count == 1) {
printf("%c", c);
}
if (feof(file)){
break ;
}
} while(1);
fclose(file);
return(0);
}
I'm not sure exactly what you're trying to accomplish, but you could try something like the following, which will print every <tag>, i.e., every string in the file between <...>'s , and will also accumulate them in an array of strings called tags[]. And note that you'd might want to add checks that avoid going over the 99 chars/tag and 999 tags total. But if this isn't anything like what you're actually trying to do, maybe clarify the question.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int ntags=0, ichar=0,nchars=0;
char c='\000', tags[999][99];
FILE *file = fopen("example.xml","r");
if (file==NULL) return(0);
while((ichar=fgetc(file))!=EOF) {
c = (char)ichar;
if (nchars==0 && c!='<') continue;
tags[ntags][nchars++] = c;
if (c=='>') {
tags[ntags][nchars] = '\000';
printf("tag#%d = %s\n",ntags+1,tags[ntags]);
nchars=0; ntags++; }
}
/* do you now want to do anything with your tags[] ??? */
fclose(file);
return(0);
}
You are trying to use a function, those parameters are char *
char *strcat(char *dest, const char *src)
but you gave strcat a char but it wants a char*
int main()
{
char str1[20] = "this";
char str2[] = "is";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
this is the way i thinkt you want it
I am attempting to create a program that will allow a user to search for a name in a file. The program does this successfully, but then it occurred to me that not everyone will type in the name as it is capitalized in the file. That is, someone may search for "sarah," but as the name is listed as "Sarah" in the file the name will not be found. To get around this I have attempted to convert both strings into upper case at the time of comparison. I am very, very new to teaching myself C, so I am not certain if I am even heading in the right direction. At this point I cannot even get the program to compile as I am getting two errors that say "array initializer must be an initializer list or string literal." I'm assuming that to mean that my syntax is not only invalid but completely in the wrong direction. What would be the best way to approach my goal?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
FILE *inFile;
inFile = fopen("workroster.txt", "r");
char rank[4], gname[20], bname[20], name[20];
printf("Enter a name: __");
scanf("%s", name);
int found = 0;
while(fscanf(inFile, "%s %s %s", rank, bname, gname)== 3)
{ char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);
if(strcmp(uppberbname,uppername) == 0)
{
printf("%s is number %s on the roster\n", name, rank);
found = 1;
}
}
if ( !found )
printf("%s is not on the roster\n", name);
return 0;
}
This two lines are wrong:
char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);
int toupper(int c); takes an int and returns an int
Because in C string is just an array of chars with a null terminator, so what you can do is to convert each character of the string to uppercase:
for (size_t I = 0; I < strlen(name); I++) {
uppername[I] = toupper(name[I]);
}
uppername[I] = '\0';
Regarding compare, you can use strcasecmp as suggested, which is Posix.
If you want to just use function in the C stdlib, convert the string as above, and then use strcmp.
toupper() works on a single character, not on a string.
No need to convert the input strings. Simple call a string case-insensitive compare.
As C does not have a standard one, it is easy enough to create your own.
int mystricmp(const char *s1, const char *s2) {
// toupper works with unsigned char values.
// It has trouble (UB) with char, when char is signed.
const unsigned char *p1 = (const unsigned char *) s1;
const unsigned char *p2 = (const unsigned char *) s2;
while (toupper(*p1) == toupper(*p2) && *p1) {
p1++;
p2++;
}
int ch1 = toupper(*p1);
int ch2 = toupper(*p1);
return (ch1 > ch2) - (ch1 < ch2);
}
use the following function, which is included in strings.h
int strcasecmp(const char *s1, const char *s2);
in your case change if statement
if(strcmp(uppberbname,uppername) == 0)
to
if(strcasecmp(bname,name) == 0)
and delete
char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);
Because the function toupper is for converting a character from small to capital, you cannot use it for a string case conversion. But you can string using the same function in this way:
while(name[i])
{
uppername[i]=toupper(name[i]);
i++;
}
while(bname[j])
{
upperbname[j]=toupper(bname[j]);
j++;
}
These statements do our string case conversion. The whole Program:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void) {
FILE *inFile;
inFile = fopen("workroster.txt", "r");
char rank[4], gname[20], bname[20], name[20], uppername[40], upperbname[40];
printf("Enter a name: __");
scanf("%s", name);
int found = 0, i = 0, j = 0;
while (fscanf(inFile, "%s %s %s", rank, bname, gname) == 3) {
while (name[i]) {
uppername[i] = toupper(name[i]);
i++;
}
while (bname[j]) {
upperbname[j] = toupper(bname[j]);
j++;
}
//char uppername[40] = toupper(name[15]);
//char upperbname[40] = toupper(bname[15]);
if (strcmp(uppername, upperbname) == 0) {
printf("%s is number %s on the roster\n", name, rank);
found = 1;
}
}
if (!found) printf("%s is not on the roster\n", name);
return 0;
}
I'm new to programming,and I have a small problem.
I have a file named questions.txt containing a string of questions, I want to read the string from the file then split it into array with each question having an index, for example a[i] = "Question i" etc.
I did so many tries, but it always ends up reading the last line in the file, when write a loop the program stops working.
This is what i came up with, it's all probably wrong:
char str[200];
char *ptr;
FILE * fp = fopen("questions.txt", "r");
while(fgets(str, 200, fp)!= NULL)
printf("%s", str);
ptr = strtok(str, "\n");
while(ptr != NULL)
{
ptr = strtok(str, "\n");
printf("%s\n", ptr);
ptr = strtok(NULL, "\n");
}
fclose(fp);
The file is:
what is your course?
who is your instructor?
Output i get is:
what is your course?
who is your instructor?
who is your instructor?
I want to read the string from the file then split it into an array with each question having an index...
Let me say, that you don't have a string to split into array.
You should better have a file with a one string of questions like this:
what is your course?:who is your instructor? // `:` is some kind of delimiter
I can suppose that you want to make a vector (one dimensional array) of the file. And in that vector, each element will contain a question from the file. Right?
I can share with you a function from my library I've made at the university. I'll write here a simple program. But it uses delimiters - :, for example. You can modify this function for working without delimiters -- this only depends on you.
In two words, this little program does the following:
// BEFORE: you have a string that ends with a null terminating character.
Question_1_abcbadsad:QUestion_2asasdasd:Question_3sldasdsa\n
^
here ^<< printing 'string' stops
// AFTER: an array of questions. Each of them ends with a null terminating character.
Question_1_abcbadsad\nQUestion_2asasdasd\nQuestion_3sldasdsa\n
^
^<< printing argz[0] will stop here
main.c
#include "argz.h"
int main()
{
error_t func;
char *argz; // pointer to a vector; to an array of questions
size_t argz_len;
// size of that vector (the size of the string you've got from the file)
// Consider `string` to be your `ptr`. You read a string from the file so
// `ptr` will point to the string.
char *string = "Question_1_abcbadsad:QUestion_2asasdasd:Question_3sldasdsa";
// Here `:` is a separate character.
func = argz_create_sep(string, ':', &argz, &argz_len);
if(func == OK)
argz_print(argz, argz_len);
else
printf("ERROR\n");
return 0;
}
argz.c
#include "argz.h"
error_t argz_create_sep (const char *string, int sep, char **argz, size_t *argz_len)
{
int i;
char *ch;
int len = strlen(string);
if(len==0)
return ENOMEM;
*argz = (char*) malloc (sizeof(char)*(len + 1));
strcpy(*argz, string);
*argz_len = strlen(*argz);
ch = *argz;
for(i = 0; i < len; i++) {
if(*ch == sep) *ch='\0';
ch++;
}
return OK;
}
void argz_print(const char *argz, size_t argz_len)
{
const char *ch;
int i;
ch = argz;
for(i = 0; i < argz_len; i++) {
if(*ch == '\0')
printf("\n");
else
printf("%c",*ch);
ch++;
}
printf("\n\n\n");
}
argz.h
#include <stddef.h> // for size_t
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef enum {OK, ENOMEM} error_t;
/* function prototypes */
error_t argz_create_sep (const char *string, int sep, char **argz, size_t *argz_len);
void argz_print (const char *argz, size_t argz_len);
I think what you want is something like this:
#include <stdio.h>
int main(){
int i=0;
char str[200],s='1'; //s is give a random character
FILE * fp = fopen("questions.txt", "r");
while (s!=EOF){ //works until s= the end of file
s=getc(fp); //s starts to receive characters from text file
str[i]=s; //each character of text is placed into the string array
i++;
}
str[i]='\0'; //s reached EOF so lets indicate where we stopped in the string
fclose(fp);
printf("%s\n",str);
//EDIT: changing 1D str to 2D str2
char str2[10][200]; // 10 for max no. of questions, 200 - length of each question
int j=0,k=0;
i=0;
for(j=0;j<200;j++){
str2[i][k]=str[j];
k++;
if (str[j]=='\n'){
i++;
k=0;}
}
for(i=0;i<10;i++) //prints your 2D string array
printf("%s",str2[i]); //after the last question there will be junk
return 0;
}
I have a struct defined as;
struct player {
int no, age;
char name[20];
} players[10];
Array is filled from file. What I try to do is, take input from user, add input to char array, send it to search(char lookup[]) function and strstr name field in a loop.
EDİT: Sorry I corrected the order. I'm trying to strstr in a loop.
char *p = strstr(players[x].name, inputFromUser);
but p is always null. How can I do this?
Thanks in advance.
EDIT - Code Added...
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct player {
int no, age;
char name[20];
} players[20];
void fillstruct(char *);
void search(char []);
int main(int argc, char *argv[])
{
int arg;
int c;
int d;
int i=0;
char a[100];
char *filename = NULL;
while((arg=getopt(argc, argv, "f:"))!=-1)
{
switch(arg)
{
case 'f':
filename = optarg;
fillstruct(filename);
break;
default:
break;
}
}
while((c=fgetc(stdin))!=EOF)
{
if(c!=10)
{
a[i]=c;
i++;
}
else
{
a[i]='\0';
search(a);
i=0;
}
}
return 0;
}
void search(char a[])
{
int i=0;
int col;
int found=0;
char *p =NULL;
while((i<20)&&(found==0))
{
p = strstr(a, players[i].name);
if(p)
{
col = p-a;
printf("\nPlayer '%s' found in '%s'.. Found index: %d", a, players[i].name, col);
found=1;
}
else
{
printf("\np=%s a=%s player[%d].name=%s", p, a, i, players[i].name);
}
i++;
}
}
void fillstruct(char *name)
{
FILE *fp;
char line[100];
int i=0;
fp = fopen(name, "r");
if(fp==NULL)
{
exit(1);
}
while(fgets(line, 100, fp)!=NULL)
{
players[i].no=i;
strcpy(players[i].name, line);
fprintf(stdout, "\nplayer=%s", players[i].name);
players[i].age=20;
i++;
}
fclose(fp);
}
Added as answer as suggested by mic_e
Assuming you're trying to search for a player name using the input from a user, you have the arguments of strstr in the reverse order. Also note that strstr is case sensitive.
char *p = strstr(players[x].name, inputFromUser);
fgets stores the \n and then stops taking input.
So suppose a player name is "user", players[i].name will be equal to "user\n" while a is "user".
So return of strstr is always NULL.
Try this instead:
p = strstr(players[i].name,a);
OR, remove the \n after taking input from file by fgets:
while(fgets(line, 100, fp)!=NULL)
{
players[i].no=i;
strcpy(players[i].name, line);
players[i].name[strlen(players[i].name)-1]='\0'; //add this line
fprintf(stdout, "\nplayer=%s", players[i].name);
players[i].age=20;
i++;
}
Like this:
char *p = strstr(players[x].name, inputFromUser);
It should work, It's fail if your input is wrong let me expalain in simple
int main()
{
char *ret;
char mystr[]="stack";
char str[]="This is stack over flow string";
ret = strstr(str, mystr);
printf("The substring is: %s\n", ret);
return(0);
}
Output is
The substring is: stack over flow string
That means
This function returns a pointer to the first occurrence in str of any of the entire sequence of characters specified in mystr, or a null pointer if the sequence is not present in str.
It case sensitive function means if try to search like
char mystr[]="Stack";//Note here first char is capital
And you got output like
The substring is: (null)
You can check your input and target string at your side by just printing content of it and verify it's correct or not.
printf("str1:%s str2:%s\n",players[x].name,inputFromUser)
char *p = strstr(players[x].name, inputFromUser);
I hope this clear your doubts.
That Should Work.
I think You have the problem with file reading Which fills the data array.
Please make sure that data you filled into structure is Ok.
And strstr returns address of the first Occurrence of the string1 in string2
where,
strstr(string2, string1);
Can anyone advise on a simple way of converting a csv string to an array of floats in C?
e.g.
char my_string[] = "1.0,2.0,3.0";
to:
my_array = [1.0, 2.0, 3.0]
where my_array is of type float[]
I would use sscanf as a quick and easy solution but I don't know how many values are contained in the string in advance
Is there some existing library function that could do this without me having to resort to looping over every char looking for a ","?
You could use strtok():
float my_array[N]; // if you don't know how many there are, use the heap
int i = 0;
char *tok = strtok(my_string, ",");
while (tok != NULL) {
my_array[i++] = atof(tok);
tok = strtok(NULL, ",");
}
There's a library you could use - LibCSV
From their description:
libcsv is a small, simple and fast CSV library written in pure ANSI
C89 that can read and write CSV data. It provides a straight-forward
interface using callback functions to handle parsed fields and rows
and can parse improperly formatted CSV files
Use a while() loop that reads only one float at a time with sscanf(). As soon as sscanf() returns 0, you know you're at the end of the list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** split(const char *str, const char *delimiter, size_t *len){
char *text, *p, *first, **array;
int c;
char** ret;
*len = 0;
text=strdup(str);
if(text==NULL) return NULL;
for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item
if(c==0) first=p; //first token top
ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL
if(ret==NULL){
free(text);
return NULL;
}
strcpy(text, str+(first-text));//skip until top token
array=ret;
for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){
*array++=strdup(p);
}
*array=NULL;
*len=c;
free(text);
return ret;
}
void free4split(char** sa){
char **array=sa;
if(sa!=NULL){
while(*sa)
free(*sa++);//for string
free(array); //for array
}
}
int main(){
char my_string[] = "1.0,2.0,3.0";
float *my_array;
char **strs;
size_t count;
strs=split(my_string, ", \t", &count);
my_array=(float*)malloc(sizeof(float)*count);
{ //convert
int i;
for(i=0;i<count;++i)
my_array[i]=(float)atof(strs[i]);
free4split(strs);
}
{ //test print
int i;
for(i=0;i<count;++i)
printf("%f\n", my_array[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getToken(char **p, const char delimiter){
char *word, *top=*p;
int len;
if(*p == NULL || **p == '\0') return NULL;
while(**p && **p != delimiter)
++(*p);
if(**p != delimiter) return strdup(top);
len = *p - top;
++(*p);
word=(char*)malloc(sizeof(char)*(len+1));
strncpy(word, top, len);
word[len]='\0';
return word;
}
int main(){
char my_string[] = "1.0,2.0,3.0";
float *my_array=NULL;
char *word, *p=my_string;
int count=0;
while(NULL!=(word=getToken(&p, ','))){
my_array=(float*)realloc(my_array, sizeof(float)*(++count));
my_array[count-1]=(float)atof(word);
free(word);
}
{ //test print
int i;
for(i=0;i<count;++i)
printf("%f\n", my_array[i]);
}
return 0;
}
You might want to look into using the strchr family of functions.
STRCHR(3) Linux Programmer's Manual STRCHR(3)
NAME
strchr, strrchr - locate character in string
SYNOPSIS
#include <string.h>
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
DESCRIPTION
The strchr() function returns a pointer to the first
occurrence of the character c in the string s.
The strrchr() function returns a pointer to the last
occurrence of the character c in the string s.
RETURN VALUE
The strchr() and strrchr() functions return a pointer to
the matched character or NULL if the character is not
found.
CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899
SEE ALSO
index(3), memchr(3), rindex(3), strpbrk(3), strsep(3),
strspn(3), strstr(3), strtok(3)