Click here to Skip to main content
15,886,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read file if the following pattern.


Here is file1.text
Abc,192.168.99.4
Eyo,192.168.8.16,3370
Opa,4447
Ctc,192.168.66.2,5507



Here is my code that is working for single , . Var3 is not displaying any value.I am putting or operation between two and three values regular expresion.


 if(EOF == fscanf(mf," %[^,],%s,%s | %[^,], %[^,] %[^,],%s,%s,%s", var1,var2,var3)){
   printf("var2=%s\n",var2);
   printf(" var1=%s\n",var1);
   printf(" var3=%s\n",var3);
}


The code is producing output
Var2=Abc

Var1=192.168.99.4

Var3=

Var2=Eyo

Var1=192.168.8.16,3370

Var3=

Var2=Opa

Var1=4447

Var3=

Var2=Ctc

Var1=192.168.66.2,5507

Var3=


What I have tried:

How to split into two comma separated into 3 values ?
Posted
Updated 25-Dec-22 23:40pm

1 solution

fscanf[^] doesn't support regular expre3ssions - it supports character classes which are a much, much more limited form.
These character classes do not include an OR operator.

Remember that C predates Regular Expressions by several decades: you will need to process the lines yourself, there is no way to automate it as you want unless you explicitly add a later regex library and process lines that way once read from the file. Google will help you find some for your environment, but I've never tried to use one so I can't recommend anything specific.
 
Share this answer
 
Comments
Member 8840306 26-Dec-22 5:46am    
I my code is in C not C++ . I am bound to use file data type .you can see this link quora.com/Can-I-use-fstream-in-C
Member 8840306 26-Dec-22 5:46am    
My question is simple how to read above text file in c using file datatype.??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900