- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- C++ file operations.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2006 03:34 AM
10-04-2006 03:34 AM
C++ file operations.
I need lib routine in C++, which update particular value in file.
I wll have, a huge file and function wuld search for Key update value of KEY to YES/NO
Can anybody help me in getting libaray which does this operation i.e open source or any good code that does so.
ABC:YES
DEF:NO
CEF:YES
GEF:KEY
It should be in c++ and efficent way of doing it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2006 03:42 AM
10-04-2006 03:42 AM
Re: C++ file operations.
could you please provide an example input and output file. It may be a lot faster to use UNIX commands to perform updates to selected lines.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2006 07:22 AM
10-04-2006 07:22 AM
Re: C++ file operations.
Example. I have file.cfg.
Where ABC, DEF,CEF etc are configuration, and have to set corresponding value for it.
ABC:YES
DEF:NO
CEF:YES
GEF:NO
But its just a snapshort. but file can be bit larger.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2006 06:28 PM - edited 10-29-2011 01:43 AM
10-04-2006 06:28 PM - edited 10-29-2011 01:43 AM
Re: C++ file operations.
Here is a C++ program (:-) that uses stdio to read a file and output the changes to stdout:
$ aCC -AA itrc_update.C
$ a.out itrc_update.in GEF maybe
ABC:YES
DEF:NO
CEF:YES
GEF:maybe
itrc_update.C:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv) {
int len;
char line[4096];
if (argc < 4) {
fprintf(stderr, "Usage: %s input key value\n", argv[0]);
return 1;
}
FILE *f = fopen(argv[1], "r");
if (!f) {
fprintf(stderr, "Can't open %s\n", argv[1]);
perror("Open failed");
return 2;
}
int len_key = strlen(argv[2]);
int len_val = strlen(argv[3]);
while (fgets(line, sizeof(line), f)) {
if (memcmp(line, argv[2], len_key) == 0 && line[len_key] == ':') {
/* overwrite value part */
memcpy(&line[len_key+1], argv[3], len_val);
len = len_key+1+len_val;
line[len++] = '\n';
} else {
len = strlen(line);
}
fwrite(line, 1, len, stdout);
}
fclose(f);
return 0;
}
As mentioned by Peter, it would probably better to use a script.