Hi,
I've some code and its doing some unusual stuff:
Here is my code: -
gstrJobname = "JobOne";
-
-
strcpy(strFile,"master_");
-
strcat(strFile,gstrJobname);
-
strcat(strFile,".dat");
-
The result "should" be "master_JobOne.dat", but it appears that either a newline or a different character is being inserted between the end of the gstrJobname and the file exitension. in the linux directory the file name appears to be:
"master_JobOne?.dat", when I attempt to download this file with an ftp client, it appears as two seperate files "master_JobName" and ".dat"
Any ideas how to perhaps clean up the hidden characters from the end of the gstrJobName. For instance, if I determine the length of that string before I build this file name, is there a way to extract this mystery character?
Thanks,
Sitko.
10 1646
How is strFile defined??
Are you overruning the array bound?? It looks like it needs to be an array of at least 18 chars.
How is strFile defined??
Are you overruning the array bound?? It looks like it needs to be an array of at least 18 chars.
char strFile[200];
gstrJobname is read in from an input file. it is also specified as char gstrJobname[200];
I read it in with this line: -
fgets(Jobname, 200, filePtr);
-
//and later move it to gstrJobname with:
-
strcpy(gstrJobname,Jobname);
-
It was my belief that fgets would cut off the trailing newline character, could this be the source of the offending hidden character?
If so, how do I get rid of it?
Thanks,
sitko.
ps I would post more of my code, but I've signed a NDA, and can't legally show more than this...
ah...I've just reread the description of fgets and I was mistaken, it includes the newline at the end of the array it reads to.
I guess my followup question would be, how do I read a line of data from a data file, into a string (char array), and leave off the ending newline? Is it posible to use 'gets' instead of fgets? Or even sprintf?
I've been reading up on these, and can find no loop-hole...
Thanks,
Sitko.
I've figured out a potential solution...although I'd still prefer it if someone came up with something better.
Using the code: -
char tempStr[200];
-
-
strncpy(tempStr,gstrJobname,(strlen(gstrJobname))-1);
-
strcpy(gstrJobname,tempStr);
-
Seems like it does the trick.
gstrJobname has "JobName\n" in it.
strncpy will copy the first X characters where X = the length of the string (minus 1), and put that into tempStr.
Then I reset gstrJobname = tempStr. and its good to go.
Feels fudgy to me, if anyone can offer something more robust...otherwise thanks for your help,
Brad.
Your solution looks okay to me. The only thing I might do differently is subtract the length of an EOL where an EOL is "\n" on some systems and "\r\n" on others.
Read a line from a data file without reading the newline character ?
I suggested using fscanf(); -
char tempArray[20];
-
-
fscanf(fp, "%[^\n], tempArray);
-
-
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
The reason fgets() retains the \n is so you can do an fputs() without worrying aout the end of line character.
gets() is for stdin only. There the \n is the user's enter key so that is converted to \0 in the receive buffer.
You can use fscanf() if job name is only one word.
Your trick of strlen(gstrJobname))-1 only works if the character following the \n is a \0. That's an assumption. I wouldn't do that.
Instead, I would use strchr to get the location of the \n and then set that location to \0.
Read a line from a data file without reading the newline character ?
I suggested using fscanf(); -
char tempArray[20];
-
-
fscanf(fp, "%[^\n], tempArray);
-
-
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
Read a line from a data file without reading the newline character ?
I suggested using fscanf(); -
char tempArray[20];
-
-
fscanf(fp, "%[^\n], tempArray);
-
-
%[^\n] means read in everything except new line character "\n" and store in tempArray via file pointer i.e. fp
Thanks, I did some more research, on fscanf and it skips "\n" from the get go...
Later when doing the windows version, I went ahead and got rid of all the fgets, in favor of fscanfs, and it worked like a charm...
Thanks,
Sitko.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nick Mudge |
last post by:
I currently have Windows XP, but I am thinking of changing my operating
system to linux because all i want to do is web development with PHP, MySQL
and Apache. Does anybody recommend the best...
|
by: matt |
last post by:
I have this code, works perfectly on Windows server, but now i'm trying
to run it on a Linux server, the form submits, i get no errors, but the
photo doesnt upload, and the caption file doesnt...
|
by: JW |
last post by:
Hi,
I am trying to see if I can display an animated .gif file
on a Java application (that is, I do not want to have
to show a series of static images to simulate animateion;
I just want to...
|
by: Parzival |
last post by:
I have a program which works well on under Windows, which
I am trying to get to work under Linux (Mandrake 9.1)
It accesses a binary file with mmap. Under Windows,
the file maps and reads withut...
|
by: Scott Simpson |
last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly
XPath book and I'm using XPath Visualizer on Windows and that seems to work
fine, but I'm looking for something on Linux....
|
by: Matthias Braun |
last post by:
Help!
I am using SuSE 9.3, php5 and Apache2 on Linux. I try to write files to
the hard drive using the following command:
$filename = "test.txt";
if (!file_exists($filename))...
|
by: B.r.a.d.l.e.y |
last post by:
A client has moved a back-end data file to a Linux machine instead of
Windows. It now will only let one user log in at a time and gives some
sort of "admin file not found" error.
My first...
|
by: gmtonyhoyt |
last post by:
I need assistance coming up with a clean way to handle filename
extensions with my application.
While I can come up with several ways of doing so on my own, I felt
perhaps it would be worth...
|
by: Franz Steinhaeusler |
last post by:
Hello NG, a little longer question,
I'm working on our project DrPython and try fix bugs in Linux,
(on windows, it works very good now with latin-1 encoding).
On Windows, it works good now,...
|
by: 8anos |
last post by:
Hello,
I am new at the community and newbie at programming :)
As you may know rapidshare provides a perl script for linux, to upload files at their servers.
You can find the original scripts at...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |