C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 4th, 2002, 07:10 AM
desmondkenny desmondkenny is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Location: Galway, Ireland
Posts: 4 desmondkenny User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question how do you go through a text file and insert a string at the end of each line?

Hey everyone, sorry about the absurdly silly question, but I've been coding this project for weeks and The little things are escaping through the burnt hole in the back of my head.

I'm building a text file of columns of numbers. the first time I go through the text file I write the first two columns......it looks something like this:

1 0.0000
2 3.0000
3 14.0000
4 110.0000
5 146.0000
6 150.0000

....see what I mean? Once that's done (and not until that's done) I need to add another column.....then another, and another, etc.

SO....I need to add a string at the end of each line.
(such as " 134.0000")

The code I have here is totally wrong, I know, and I know why (because when you open a file in "r+", it appends, it won't insert)

any simple ideas.....?


if(j == 0)
{
i=0;
while(i<count)
{
i++;
sprintf(buffer, "%d", i);
fprintf(file2, " %s %s\n", buffer, aStringOfDouble);
}

fclose(file2);
}
else
{
file2 = fopen(ipname, "r+");

i=0;
while(i<count)
{
i++;

for(c=0;c<=j;c++)
{
fscanf(file2, "%s", buffer);
}

fprintf(file2, " %s", aStringOfDouble);
}

fclose(file2);
}

Reply With Quote
  #2  
Old March 4th, 2002, 07:35 AM
costas costas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 7 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi,

i am just learning c++, but handled textfiles with perl a lot. If I got you right line 2 of your example should afterwards look something like:

2 3.0000 134.0000

?

I think you should read in the lines via getline().


include<iostream>
include<fstream>

string buffer;
ifstream in("file.txt");
//...

getline(in, buffer);



I don't know wether the filepointer is moved automatically after reading in the line, but you can modify the line and afterwards write it back.

This applies only if you use c++, of course, as fstream is a class, I think.

Hope it didn't do any harm...

Reply With Quote
  #3  
Old March 4th, 2002, 07:46 AM
desmondkenny desmondkenny is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Location: Galway, Ireland
Posts: 4 desmondkenny User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks, but It has to be in C, not C++

Reply With Quote
  #4  
Old March 4th, 2002, 11:01 AM
costas costas is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 7 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok,

I dug through some references and found the following (still in search for the linewise attempt):

use fgets()!


char Data[80]; //for example, your buffer could take a different value

char *ReturnCode;
FILE *FilePointer;

ReturnCode=fgets(Data, 80, FilePointer);

ReturnCode == NULL when an error occours. The EOF is
considered to be an ERROR!


this way you could do:

while (fgets(line, sizeof(line), fp))

where line is a char-array of a certain size (255, e.g.) and fp is your filepointer. As fgets() returns NULL at the end of the file you read in all the lines with the while.

You could for example buffer the complete file in an twodimensional char-array and overwrite it afterwards or write your modified lines when still within the while by defining an output-filepointer to write to.

Not?


As I said, I am a beginner myself, but that looks fine to me.

Reply With Quote
  #5  
Old March 27th, 2002, 06:44 AM
CodeBastard CodeBastard is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Sweden
Posts: 0 CodeBastard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to CodeBastard
Here is a very simple example on how you can add another column to your file.
For simplicity I have forgone all error checking.

Code:
#include <stdio.h>

int main(int argc, char **argv) {
    FILE *fp;
    FILE *tmp = tmpfile();
    char *p;
    char *more = "foo";
    char line[1024];
    /* First put contents of file into temporary file */
    fp = fopen("file.txt", "r");
    while ((p = fgets(line, 1024, fp))!=NULL) {
        fputs(line, tmp);
    }
    fclose(fp);
    rewind(tmp);
    /* Reopen file now with write permissions */
    fopen("file.txt", "w");
    while (( p = fgets(line, 1024, tmp))!=NULL) {
        line[strlen(line)-1] = '\0'; /* Clear away newline */
        sprintf(line, "%s %s\n", line, more);
        fputs(line, fp);
    }
    fclose(fp);
    fclose(tmp);
    return 0;
}


Hope this helps you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > how do you go through a text file and insert a string at the end of each line?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway