473,382 Members | 1,611 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,382 software developers and data experts.

Read a line by characters

I'm reading line by line and making comparisons. I have been using the index function but I knew what my substring was. Now I don't know what my substring is. I have an index function searching for ' /* ' and as soon as I see this comment, I want to know if there is anything before it on the line.

For example:

Expand|Select|Wrap|Line Numbers
  1. print "hello"; /* prints hello */
  2.  
I'm trying to determine if there is a line of code before the comment starts. Now I have this approach done by searching for a semicolon. Then comparing the index of the semicolon to the index of /* and checking if the index of the semicolon is lower than the the index of the /* which means that there is a line of code. Now I have this problem what if there is no semicolon there such as

print "hello" /* prints hello */
;

This line of code will still execute but I will not know if there is something before the /*. My question is, is there someway of reading the first character in the line and comparing that to / to determine if there is something before the /*

Psuedocode:

Expand|Select|Wrap|Line Numbers
  1. if (index($_, "/*"))
  2. {
  3. #now check to see if there is something before it
  4.  
  5. if(first character of line == "/")
  6. {
  7.     print "yes there is something before /* ";
  8. }
  9.  
  10. }
  11.  
Any help would be greatly appreciated. Thanks in advance. In the meantime I will continue to look for a solution. If I find one, I will post the answer.
Apr 10 '08 #1
2 1220
KevinADC
4,059 Expert 2GB
Since you are really searching for a pattern a regexp is appropriate:

Expand|Select|Wrap|Line Numbers
  1. @lines = ('print "hello"; /* prints hello */', 'print "hello";');
  2. foreach my $line (@lines) {
  3.    if ($line =~ m#^.+?/\*.*?\*/#){
  4.        print "There is a comment: $line\n";
  5.    }
  6.    else {
  7.        print "There is no comment: $line\n";
  8.    }
  9.  
I didn't put too much thought into the regexp so it could maybe be improved upon.
Apr 10 '08 #2
Thanks a lot for the solution, I looked into it when you gave me the recommendation for the regex. This is the solution I came up with. I strip all the leading whitespaces and check if it's a comment or not. That's how I know it's a line of code. There is one flaw if the comment is before the line of code (/* */ code;) But that's a case that I'm not looking for.

Expand|Select|Wrap|Line Numbers
  1.  
  2. s/^\s+//; #strip leading whitespace
  3.  
  4. if(/^[^\/*]/)
  5. {
  6.     $line_count++;
  7. }
  8.  
  9.  
Apr 16 '08 #3

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

Similar topics

3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
9
by: Carramba | last post by:
#include <stdio.h> int main( void ){ char cQuit = 'a'; char cKommando , artistNamn , skivansNamn , cChar; int cK ; FILE *pekaFile; printf( "l - for read file\n"); printf( "s - for writte...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.