Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,161 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




Processing line by line / writing string by string

 
Reply to this topicStart new topic

Processing line by line / writing string by string

backfire32
5 Dec, 2007 - 04:43 PM
Post #1

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 6


My Contributions
Hi i have been working on this C program assignment for awhile now and the only part of this assignment i can't get to process right is for it to get the next line from the inputed files to run in my loops to print to a new created file. I am new to programming

The program is setup to read two .txt files. "requests.txt" and "toys.txt" line by line and process them. Then write the processed strings to new file "gifts.txt"

If the first line "string" in request has one word it will attach the first line from toys.txt to it Then write it to new file "gift". If request has two words it prints the sting as is to gift. If just a blank line is read from request it goes to the next line.
...........................................................................................................
requests.txt example

Brian
John Airplane

Jack
Paul Money

.........................................................................................................
gifts.txt example
Car
Laser
Computer
Flashlight
..........................................................................................................
CODE
/*My program*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CHAR 1000


int main (void)
{

char inarray1[CHAR];
char inarray2[CHAR];
char *x;
FILE *request,*toy,*gift;

if ((gift = fopen("gifts.txt","w")) == NULL)
{exit(1);}

if ((request = fopen("requests.txt","r")) == NULL)
{exit(1);}
while ( fgets(inarray1,CHAR,request) != NULL)
fclose (request);

if ((toy = fopen("toys.txt","r"))== NULL)
{exit(1);}
while (fgets(inarray2,CHAR,toy) != NULL)
fclose (toy);

x = inarray1;

while ((*x == ' ') || (*x == '\t')) x++;
{
    if ((*x == '\n') || (*x == '\0'))
    {
        fgets(inarray1,CHAR,request);    
    }
}

while ((*x != ' ' && *x != '\t') && (*x != '\n' && *x != '\0'))x++;
        
while ((*x == ' ') || (*x == '\t'))x++;
    {    
        if ((*x != '\n') && (*x != '\0'))
        {
            fprintf (gift,"%s",inarray1);
            fgets (inarray1,CHAR,request);
        }
        else
        {
            *x = '\0';
            fprintf (gift,"%s\t",inarray1);
            fprintf (gift,"%s",inarray2);
            fgets (inarray1,CHAR,request);
            fgets (inarray2,CHAR,toy);        
        }
    }


fclose (gift);    
return 0;

}

.............................................................................................................


When i process using my program i only get the first line output to the newly created file gifts.txt and i am not sure what im doing wrong or not doing to make it do the rest of the files.

My output would just read

Brian Car

Any insight i would really appreciate!


*Mod Edit: added code tags. code.gif
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Processing Line By Line / Writing String By String
5 Dec, 2007 - 07:22 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Sorry, I can't quite understand what you are trying to do.

Could you post both requests.txt and toys.txt (within [ code ] [ /code ] tags) please, and try explaining again what you are trying to do.

Thanks.
User is offlineProfile CardPM
+Quote Post

backfire32
RE: Processing Line By Line / Writing String By String
5 Dec, 2007 - 08:00 PM
Post #3

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 6


My Contributions
CODE
      
/*request.txt*/
Andy
Bob  Tent
  Cindy
   Doug        Transformers
Elaine     Coloring_book
Fran  Doctors_kit
  Greg
   Helen
Jason              Dinosaur
Louise
  Mark
   Nancy
    Phil
    Sam                           Detective_kit
        Ted
    Victor Swiss_army_knife
Zack


Anette            Barbie
Bertrand
Colombe
     Francois    Toy_truck
Isabelle
Jacques Transformers
Lucien
Marie           Nurse_costume
Serge
Yvonne   Dolhouse

Chengcui
    Heping       Lego
Jiayang      
    Lei
Mei-Ling                  Ice_skates
Qiang   Harry_Potter_book
Shengyu
        Tong
Xiangyu
    Ying
Zheng

            

Alejandro
AnaMaria
Blanca
Diego             Dinosaur
Emiliano
                 Fernando
Guillermo
Jacinta      Plush_bear
Karlos
Luis Cell_phone
Manuela
Pablo         Skateboard
Roberto Jet_plane
         Simon
Teresa

Asha
Chetana
  Gautam
   Indira    Ballerina_shoes
    Kalyan
     Lakshmi
      Nirmal DVD_player
       Praveen    Train
        Rajiv
         Sanjay        Bazooka
          Usha                     Space_alien
           Vinay

Anke
Bettina            Barbie
Claudia
Friedrich
  Guntram Space_station
  Hans
Ilse
Jurgen       Dinosaur
Karl
Manfred                                Bow_and_arrows
Reinhard
Silke                 Bicycle
Torkel Basketball
Ulrich                         Porshe
Wolfgang

            Abdul
        Aysha
        Bahram
  Fatima                       Dollhouse
     Hassan        iPod
   Kemal
Mohammad
            Murad
Nasser                                     Computer
   Omar
Selim
         Yakub
Walid

              Aleksey
             Dmitry
            Eugenia
           Feodor             Dinosaur
          Ivan                  Digital_camera
         Josif
        Masha     Handbag
       Nikita
      Olga
     Piotr    Rocket_launcher
    Sasha
   Tanya
  Varya     Barbie
Yuri                                                   Dinosaur
Zlata

Aaron
Aviv
    Golda
Isaac                Batman_action_figure
Moshe
        Rebecca
Shimon    Football





CODE

/*toys.txt*/
Action_figure
Barbie
  Crayons
   Doll
    Elephant_figure
     Fire_truck
      Helicopter
iPod
Mask
  Plush_bear
   Rocket
    Spiderman_figure
     Train_set
      Batman_figure
Baseball
Electric_car
  Fishing_rod
   Lego
    Megablocks
     Robot
      Snowboard
Transformers
Water_gun
  Astronaut_gear
   Dollhouse
    Football
     Guitar
Lava_lamp
Matchbox_car
  Nintendo
   Rollerblades
    Skates
     CD_player
Bow_and_arrows
Cowboy_gear
  Bicycle
   Road_construction_set
    Telescope
     Night_googles
Flute
Candy
  Board_game
   Globe
    Star_Wars_set
Action_figure
Barbie
  Crayons
   Doll
    Elephant_figure
     Fire_truck
      Helicopter
iPod
Mask
  Plush_bear
   Rocket
    Spiderman_figure
     Train_set
      Batman_figure
Baseball
Electric_car
  Fishing_rod
   Lego
    Megablocks
     Robot
      Snowboard
Transformers
Water_gun
  Astronaut_gear
   Dollhouse
    Football
     Guitar
Lava_lamp
Matchbox_car
  Nintendo
   Rollerblades
    Skates
     CD_player
Bow_and_arrows
Cowboy_gear
  Bicycle
   Road_construction_set
    Telescope
     Night_googles
Flute
Candy
  Board_game
   Globe
    Star_Wars_set
Action_figure
Barbie
  Crayons
   Doll
    Elephant_figure
     Fire_truck
      Helicopter
iPod
Mask
  Plush_bear
   Rocket
    Spiderman_figure
     Train_set
      Batman_figure
Baseball
Electric_car
  Fishing_rod
   Lego
    Megablocks
     Robot
      Snowboard
Transformers
Water_gun
  Astronaut_gear
   Dollhouse
    Football
     Guitar
Lava_lamp
Matchbox_car
  Nintendo
   Rollerblades
    Skates
     CD_player
Bow_and_arrows
Cowboy_gear
  Bicycle
   Road_construction_set
    Telescope
     Night_googles
Flute
Candy
  Board_game
   Globe
    Star_Wars_set

  



This post has been edited by backfire32: 5 Dec, 2007 - 07:59 PM
User is offlineProfile CardPM
+Quote Post

backfire32
RE: Processing Line By Line / Writing String By String
5 Dec, 2007 - 08:07 PM
Post #4

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 6


My Contributions
The deal is if request.txt has two words "name and toy" then u simply print the line into the new file your writing to "gifts.txt".

If the line it is reading from request.txt has only a name, then you need to attach a toy to that name from the toys.txt file.

And if a line in requests.txt is empty or has nothing then you go to the next line in that file. the process should repeat until all of request.txt has been processed.

As its doing this you skip the extra white spaces at the begining of the line. And all the processing of if the line has one or two words is done though the while loops. The "if" and "else" on the last while loop determine if a toy should be added to the line its reading or if it should simply print the line as it is.

I can't get it to process past the first line. when i run the program i only get the first string

"Andy Action_figure"

after that my program is not returning to process the other lines. i don't know what im doing wrong. blink.gif

This post has been edited by backfire32: 5 Dec, 2007 - 08:13 PM
User is offlineProfile CardPM
+Quote Post

backfire32
RE: Processing Line By Line / Writing String By String
5 Dec, 2007 - 09:39 PM
Post #5

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 6


My Contributions
anybody have a clue? i think the deal with it is its not returning the next line in
in the file to be processed and i don't know how to do that.
User is offlineProfile CardPM
+Quote Post

backfire32
RE: Processing Line By Line / Writing String By String
6 Dec, 2007 - 04:09 PM
Post #6

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 6


My Contributions
Bump. Any ideas? :S
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month