473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

To replace a certain string in a line

Hi everyone,

i met some difficulties handling the following problem. Say I have a input file in following format:
File Name File Number Size(blocks) Size(bytes)
file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288

My program suppose to simulate the rename operation on file.
For example, when u run "ren file1 file0", the input file will become as following:

File Name File Number Size(blocks) Size(bytes)
file0 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288

What I am able to do now is just insert the new file name "file0" in front of the old name in the input file,such as this:

File Name File Number Size(blocks) Size(bytes)
file0 file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288

so how can I delete the old file name? Any help would be greatly appreciated.

My current code is as following:

Expand|Select|Wrap|Line Numbers
  1. main(int argc, char **argv){
  2.  
  3.     FILE *list,*tmp;
  4.     char line[80];
  5.     char *pch;
  6.  
  7.  
  8.     if(argc!=3){
  9.         perror("You must have 2 argument.\n");
  10.         exit(1);
  11.     }
  12.  
  13.     //generate the tmp list file
  14.     if((tmp = fopen("tmp.txt", "w+"))==NULL){
  15.         printf("Cannot create temp list file");
  16.     }
  17.  
  18.     //do the actual rename
  19.     if(rename(argv[1], argv[2])) 
  20.         printf("Rename failed.\n");
  21.  
  22.  
  23.     //rename the entry in the table
  24.     if((list = fopen("filelist.txt", "r+"))==NULL){
  25.         printf("Cannot open the list file");
  26.     }    
  27.  
  28.  
  29.     while(fgets(line, sizeof(line), list))
  30.     {
  31.         printf("Line read: %s\n", line);
  32.  
  33.         if(strstr(line, argv[1]) !=NULL)
  34.         {
  35.             printf("Replacing.\n");
  36.             strcpy(line, "");
  37.             strcat(line, argv[2]);
  38.         }
  39.  
  40.         fputs(line, tmp);
  41.     }
  42.  
  43.  
  44.     fclose(list);
  45.     fclose(tmp);
  46.  
  47.     //remove filelist.txt
  48.     if (remove("filelist.txt")){
  49.             printf("Cannot delete file.\n");
  50.             exit(1);
  51.     }
  52.  
  53.     //rename tmp.txt to filelist.txt
  54.     if(rename("tmp.txt", "filelist.txt")) 
  55.         printf("Rename failed.\n");
  56.  
  57. }
Mar 18 '08 #1
1 1373
whodgson
542 512MB
Maybe you could use the replace() function defined in the <algorithm> header and code s+4 to be replaced by "1" These functions are referred to as Standard C++ Generic Algorithms in a book i have read at least 19 times called Programming with C++ by John R Hubbard.
Mar 22 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
0
by: xtra | last post by:
Hi Everyone This post follows fromt the post "do I need to set objects to nothing". Here is a function that I used to add a line of code to all procedures. It is pretty rought, but it seemed...
6
by: localhost | last post by:
I have a string that looks like this: "document.form1.textBox1.focus ();document.form1.textBox1.select();" I want to replace the text between "document.form1." and ".focus()", as well as...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
7
by: rinkudhimar | last post by:
I am finding certain text and replacing it in the file. But it gets written three times in the file. I am sending the code. *******************HTML CODE********************************* <!DOCTYPE...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.