ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Not sure what you mean really but keeping things simple, you might try to read the file (or paste the text content) into your favourite text editor and use the 'Search & Replace' function ... ? I spent ages once trying to achieve a similar issue in a spreadsheet only to later realise that this simple solution was easier.
Not sure what you mean really but keeping things simple, you might try to read the file (or paste the text content) into your favourite text editor and use the 'Search & Replace' function ... ? I spent ages once trying to achieve a similar issue in a spreadsheet only to later realise that this simple solution was easier.
If every line in your file follows a consistent format, you can perform sscanf() on the input line.
Code:
int ret;
if((ret = sscanf(input_buffer, "%s:%s:%s:")) != 3) {
printf("Error: did not find correct format, ret=%d on line %s\n", ret, input_buffer);
}
Plus, make a test program, turn off optimization, enable GDB debugging when you compile, save returns and information as much as possible in variables, and use GDB to debug the process line by line.
If every line in your file follows a consistent format, you can perform sscanf() on the input line.
Code:
int ret;
if((ret = sscanf(input_buffer, "%s:%s:%s:")) != 3) {
printf("Error: did not find correct format, ret=%d on line %s\n", ret, input_buffer);
}
Plus, make a test program, turn off optimization, enable GDB debugging when you compile, save returns and information as much as possible in variables, and use GDB to debug the process line by line.
Hi,
My file has only one line so i am thinking of using fscanf to parse the lines.
i am using fscanf(fp,"%s %d", str,&num). But if my file has something like "abcd 1234". It is not working , how to configure format specifier for this scenario?
Not sure what you're asking. I realize that I did overlook the fact that scanning "012345" as an integer would result in 12345. This does work if the line of data is "abcd 1234" and gives you "abcd" as a string and 1234 as a number.
Copy that code, compile it, and test it for yourself.
If you're saying that you'd like to write that data out to a file. I'd recommend you write it to a different file, for starters. Because opening a file in append mode versus create-read/write mode have different behaviors insofar as what does or doesn't happen to the existing file contents as well as where the read and write pointers start out at.
But if you just fopen() a different file with "w" then it will always create a new file and start writing at the top of that new file. It will also always overwrite any same named file.
I suggest that you should develop an understanding about reading and writing separately first before you play with modifying in place.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.