File Program issue in c


 
Thread Tools Search this Thread
Top Forums Programming File Program issue in c
# 1  
Old 04-15-2014
File Program issue in c

Code:
B17 -> B19	 [label="{10}"];
B17 -> B21	 [label="{4}"];
B18 -> B19	 [label="{4}"];

Can any one help with the pointer solution to check for this -> symbol and one it finds the symbol it should move behind 4 characters and store the string
B17 in an array and then move 4 characters ahead and similarly save B21 likewise for each line do as above..

Last edited by Franklin52; 04-15-2014 at 03:05 AM.. Reason: Please use code tags
# 2  
Old 04-15-2014
Code:
void foo( char *src, char *behind, char *infront)
{
    char *p=strstr(src,"->");
    *infront=*behind=0x0;  // initialize to empty
    if(p!=NULL)
    {
         char *q=p;
         q-=4;                 // go four positions backward
         memcpy(behind, q, 3);
         behind[3]=0x0;  // mark the end of the string
         q=p;
         q+=4;
         memcpy(infront, q, 3);
         infront[3]=0x0;
    }
}

This is not succinct code but rather code to show how to do your task. Next time you should show what you have tried. Also this does zero error checking, e.g., what if you had B17-> in your file?
# 3  
Old 04-16-2014
Code:
#include<stdio.h>
int main() {
  	char ch, file_name[25];
 	char wordChoice[2]="->";
 	int count=0,z=0,i=0;
    	FILE *fp;
 	char *a[100];
        fp = fopen("mergesort1.txt","r");
        if( fp == NULL )    {
       perror("Error while opening the file.\n");
           }

      printf("The contents of %s file are :\n", file_name);
      char buffer[2024]; //Sorry for this.

      while(fgets(buffer, 2024, fp) != NULL)
      {
               if(z==1)
               {
                       printf("%s",buffer);
                       z=0;
               }
               if(strstr(buffer, wordChoice))
               {count++; 		z=z+1;   }
          }
      fclose(fp);
   	printf("count:%d \n",count); 	 	  }

This is the code which searches for a string ie(->)in a file which is attached below and on finding it prints the next word after the symbol.In this code how is it possible to print the previous word before the symbol and save it in an array.

eg the file is this:
stylefilled
B02
fillcolorffe6e6
shapecircle
B01
->
B02
B03
shapecircle
B02
->
B03
B04
shapecircle
B03
->
B04
B05
shapecircle
B04
->
B05
B06
fillcolorffe6e6
shapecircle
B05
->
B06
B07
shapecircle
B06
->
B07
B08
fillcolorffe6e6
shapecircle
B07
->
B08

Last edited by Corona688; 04-16-2014 at 12:43 PM.. Reason: Linefeeds missing!
# 4  
Old 04-16-2014
The file you show is line-oriented and you read it with fgets which is line-oriented. Yet, in your task description you refer to previous word / next word, which is a bit different concept in my mind.

In any case, for you to print previous "word" (in effect, previous line) just have another
Code:
char prev_buf[2024];

variable and at the end of the loop always copy buffer into it, like
Code:
 while(fgets(...))
 {
   ... your code here ...
   strcpy(prev_buf, buffer); 
 }

This will allow you to print the previous buffer when you encounter the key word, as in your code
Code:
 
 if(strstr(...)) { printf("%s\n", prev_buf); up your counts etc }

# 5  
Old 04-16-2014
Quote:
Originally Posted by migurus
In any case, for you to print previous "word" (in effect, previous line) just have another
Code:
char prev_buf[2024];

variable and at the end of the loop always copy buffer into it, like
Code:
 while(fgets(...))
 {
   ... your code here ...
   strcpy(prev_buf, buffer); 
 }

This will allow you to print the previous buffer when you encounter the key word, as in your code
There's no need for that strcpy. If you have two buffers, using pointers you can simply swap prev and current.

Regards,
Alister
# 6  
Old 04-17-2014
yes sir we were also thinking of having two buffer but i m not getting idea how to proceed with it..
# 7  
Old 04-17-2014
Quote:
Originally Posted by zinat
yes sir we were also thinking of having two buffer but i m not getting idea how to proceed with it..
I'm curious. Why are you using C? Does this program do anything besides process text? If not, and if this code is going to be used in production, given your level of proficiency, it would be wise to use a safer, higher level language which can manipulate strings without you having to directly manage memory and pointers.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

2. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

3. Homework & Coursework Questions

Fortran program issue

1. The problem statement, all variables and given/known data: I was given this program to work with that is supposed to work as it is. It comes with a makefile so all I should have to do is use the make command, then type ./blub to execute the program. A list of data should then appear in the... (3 Replies)
Discussion started by: Arjani
3 Replies

4. Shell Programming and Scripting

Need assistance with a file issue and a terminal issue

Hello everyone, I'm in need of some assistance. I'm currently enrolled in an introductory UNIX shell programming course and, well halfway through the semester, we are receiving our first actual assignment. I've somewhat realized now that I've fallen behind, and I'm working to get caught up, but for... (1 Reply)
Discussion started by: MrMagoo22
1 Replies

5. Solaris

Issue in pro*C program compiled in solaris 10

Hi, We upgraded our servers from solaris 9 to 10. We recompiled all the Pro*C programs with the new oracle version as well. Oracle is 11g. We are facing core dump with the below error for certain executions. But when we are placing new statements between the error fucntion we get junk values to... (1 Reply)
Discussion started by: saroopkris85
1 Replies

6. Programming

issue with fastcgi program

Hello, I am strugglign with the following fastcgi C++ program with access to a postgresql database through the SOCI library. All the components work individually properlyy but the combination does not. The program compiles fine but the Apapche 2.2 error log files contains: FastCGI: comm... (0 Replies)
Discussion started by: JCR
0 Replies

7. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

8. UNIX for Dummies Questions & Answers

AWK Program File Help

I have some .dat files that I cannot open and read the data. It is an awk program file, and my question would be to you all is there a way to convert this awk file to ascii text? Thanks (10 Replies)
Discussion started by: ryangfm
10 Replies

9. UNIX for Advanced & Expert Users

Setuid Program with (-rwsr-sr-x 1 root other ) UID/EUID issue

Hi, I have a program with the following suid setup -rwsr-sr-x 1 root other 653 Aug 16 17:00 restart_server It basically starts up a service that has to be started by root. I just want the normal users to be able to restart the service using the script above. But when the... (7 Replies)
Discussion started by: 0ktalmagik
7 Replies

10. Windows & DOS: Issues & Discussions

program to rename file name

I need a program to rename file name. so the input to run the program would be RenameFiles //server1/Folder1/folder2/test*er.doc program would return: ++server1+Folder1+folder2+test*er.doc Can some one please help me? (1 Reply)
Discussion started by: randyzapata
1 Replies
Login or Register to Ask a Question